Albus Dumbledore
Albus Dumbledore

Reputation: 12616

explicit require in Rails 3

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!

Upvotes: 4

Views: 1001

Answers (1)

bensie
bensie

Reputation: 5403

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)

Upvotes: 5

Related Questions