Adding lib to 'config.autoload_paths' in Rails 3 does not autoload my module

Ruby on-Rails

Ruby on-Rails Problem Overview


I place a file name g.rb in side Rails.root/lib folder The file content is like this:

module Google
end

Then I add

config.autoload_paths += %W(#{config.root}/lib #{Rails.root}/app/delayed_jobs)

to my Rails.root/config/application.rb

However, when I try to invoke Google from rails console, an exception is thrown. The exception goes away only if I execute require 'google'. Why? Shouldn't my file is autoloaded and shouldn't I access the module without any extra require statement?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Hmm, I discovered an interesting thing. In order for Rails to auto load my class, the class name should be compliant to the file name and the folder structure. For example, if I want to have Google module autoloaded, I must placed it inside google.rb, directly under /lib (incase I specify autoload from /lib). If I want to auto load Google::Docs, then I either place it inside google.rb or google/docs.rb

Solution 2 - Ruby on-Rails

I had a similar problem with getting my module to run on Heroku. In addition to the autoload naming convention stated by Stephen C, I found out that the module code must be require'd due to a threadsafe assumption made by the Rails' production environment on Heroku (even though threadsafe was commented out in my production.rb configuration file.) As soon as I require'd the module file before calling include on the module, everything started to work.

require 'mymodule'
include Mymodule

Please take a look at this excellent article on the subject of getting Modules to load correctly in Heroku (production).

Solution 3 - Ruby on-Rails

That's because the point of autoload is not to 'require' everything up front (startup penalty). Classes are loaded as they are needed/referenced. In order to do this, you need some way to know where to look for the class. Otherwise, you would have to load every file in the autoload directory in advance to see what classes are declared. It's a tradeoff, but requiring everything in advance (as marbaq suggests) is not autoloading. You can use the autoload command as provided by Ruby, which takes two arguments, the module to load (symbolized, i.e. :Google in your case), and the second argument is the filename, which would be g.rb if lib is in your load path ($:). See the Ruby docs for autoload.

Solution 4 - Ruby on-Rails

Change config.autoload_paths to config.eager_load_paths

(based on Rails issue #6850 and Force reload! from lib directory in rails 3.2 console)

Solution 5 - Ruby on-Rails

I faced the same problem just now, and my "solution" (or rather workaround) was to manually require every needed file from Rails.root/lib in my application.rb.

require 'lib/message'
require 'lib/store'
require 'lib/vault/vault.rb'
require 'lib/custom_loggers'

module MyApplication
  class Application < Rails::Application

My next step would be to categorize the files in module folders as you mention.

Solution 6 - Ruby on-Rails

i found this solution recently

config/application.rb

module AppName
  class Application < Rails::Application

    # Custom directories with classes and modules you want to be autoloadable.
    config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
    config.autoload_paths += Dir[Rails.root.join('app', 'lib', 'extensions')] 

  end
end

the first config call induces rails to auto-load all sub-directories of the app/models directory so now i can have /app/models/sub_directory/model.rb auto-loaded (handy for organising an app with a large code base)

the second config call induces rails to autoload the lib/extensions directory

hope this helps

note: i believe this is rails 3 specific

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhương NguyễnView Question on Stackoverflow
Solution 1 - Ruby on-RailsPhương NguyễnView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDon LeathamView Answer on Stackoverflow
Solution 3 - Ruby on-RailsStephen CView Answer on Stackoverflow
Solution 4 - Ruby on-RailsE. SamboView Answer on Stackoverflow
Solution 5 - Ruby on-RailsFredrik BoströmView Answer on Stackoverflow
Solution 6 - Ruby on-Railsben.mView Answer on Stackoverflow