views:

16

answers:

1

I am converting my Rails 2 app to Rails 3. So far, I've been successful. However, there is this strange issue that I have to explicitly require any external files. Here is my original (i.e. Rails 2) ActiveRecord model:

class Book < ActiveRecord::Base
  belongs_to :author
  has_many :translations, :dependent => :destroy
  include Freebase
...
end

in order to make it working in Rails 3, I have to require the model Translation and Freebase.rb file, thus:

class Book < ActiveRecord::Base
  require File.expand_path(File.dirname(__FILE__) + '/translation.rb')
  belongs_to :author
  has_many :translations, :dependent => :destroy
  require File.expand_path(File.dirname(__FILE__) + '../../../lib/freebase.rb')
  include Freebase
  ...
end

Is it the normal way in Rails 3, or I am doing something wrong. In other words, why is it necessary to explicitly include those files? There might probably be some reason for the Freebase.rb file, which is placed in the lib folder, but what about the Translation model which is in the same dir?

Thanks guys!

+4  A: 

Rails 3 doesn't automatically autoload quite as much as Rails 2 did.

Open up config/application.rb and customize the line that looks like:

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)

In your case, you probably want to have

config.autoload_paths += %W(#{config.root}/lib)
bensie
Thanks a lot! What about the fact that I need to load the files of my models, too? Rails doesn't recognise JSON, too, even though it's built in. Funny stuff happening!
Albus Dumbledore
About JSON - it seems that I need to call it like so: `ActiveSupport::JSON` and it'd work.
Albus Dumbledore