15

I have a class ConstData:

class ConstData

  US_CITIES = ['miami', 'new york']

  EUROPERN_CITIES = ['madrid', 'london']

end

Its stored under /lib/const_data.rb

The idea is that inside a model, controller or view I can do:

ConstData::US_CITIES to get the US_CITIES etc

Rails should load this class automatically, I got this from: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/662abfd1df9b2612?hl=en

However this does not work. Can anyone explain me how to accomplish this ?

daniel
  • 9,732
  • 7
  • 42
  • 57

4 Answers4

48

The post @daniel refers to is from 2008. Rails has changed since then.
In fact, quite recently. Rails3 doesn't load the lib/ directory automatically.

You can reactivate it quite easily though. Open config/application.rb And add, in the config (in the Application class) the followin :

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

Then your lib/ dir will be autoloaded.

Jared Beck
  • 16,796
  • 9
  • 72
  • 97
Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
  • Thank you sir, I'll accept it in 6 mins. Anyway can you also tell me how to check what is in your autoload path ? – daniel Nov 01 '10 at 23:17
  • 2
    <>::Application.config.autoload_paths – Morgan Christiansson Jan 12 '11 at 12:24
  • 6
    Warning! Many of the kinds of modules you might have in /lib have configuration information set in initializers, and that configuration information will be dropped when the module is reloaded, causing inexplicable failures in development of things that work fine in testing and production. The solution to that is adding the /lib path to config.autoload_once_paths instead of to config.autoload_paths . – Steve Jorgensen Dec 06 '11 at 04:00
  • 4
    Also worth noting is that the file name is important. It doesn't apply to this exact example, but it's related, and it took me a long time to realize my mistake. See: http://stackoverflow.com/questions/4074830/adding-lib-to-config-autoload-paths-in-rails-3-does-not-autoload-my-module – Jon Smock Dec 10 '11 at 17:02
13

The reason autoload_paths didn't work for you and you were forced to do:

Dir["lib/**/*.rb"].each do |path|
  require_dependency path
end

is because you forgot to namespace your class.

lib/awesome/stuffs.rb should contain a class/module like this:

class/module Awesome::Stuffs
....

but you had:

class/module Stuffs
....

Rails can only autoload classes and modules whose name matches it's file path and file name.

:)

Steven Bristol
  • 169
  • 1
  • 3
  • Nice. This hadn't mentioned earlier and caused me a lot of problems. For the load path to match if I have a compound noun as the module name what should I do? (i.e. lib/monkey_man/engine.rb --> module MonkeyMan::Engine?) Does it automatically caml case it? – Vivek Sep 14 '12 at 17:54
2

config.autoload_paths did not work for me. I solved it by putting the following in ApplicationController:

Dir["lib/**/*.rb"].each do |path|
  require_dependency path
end
Benoit Garret
  • 14,027
  • 4
  • 59
  • 64
1

Follow the solution for lib dir be autoloaded:

Remove config.threadsafe! from development.rb and production.rb;

Add in config/application.rb:

config.autoload_paths += %W(#{config.root}/lib)
config.threadsafe!
config.dependency_loading = true
Victor Lellis
  • 1,304
  • 1
  • 14
  • 25
  • Thank you. I had added `confing.threadsafe!` in a flurry of changes to try to get wicked_pdf to work and that was the reason my auto load stopped working. – TheRightChoyce Jul 15 '13 at 19:03