Jigar Patel Driving fast in a slow lane!

Rails 3 Does Not Auto-Load Classes/Modules From "/lib" Folder

September 15, 2010 / Jigar Patel

In Rails 3 classes and modules from “/lib” folder are not automatically loaded. If you create any module/class in lib folder and try to use it, the server will throw an “uninitialized constant XYZ (NameError)”.

The change is deliberate and there is a configuration setting in application.rb to specify the folders which are to be auto loaded.

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

Uncomment these lines and modify the path to 

config.autoload\_paths += %W(\#{config.root}/lib)

This will autoload all the classes and modules in the “/lib” folder.