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 ?
config.autoload_paths
did not work for me. I solved it by putting the following inApplicationController
:Follow the solution for lib dir be autoloaded:
Remove
config.threadsafe!
from development.rb and production.rb;Add in
config/application.rb
:The reason autoload_paths didn't work for you and you were forced to do:
is because you forgot to namespace your class.
lib/awesome/stuffs.rb should contain a class/module like this:
but you had:
Rails can only autoload classes and modules whose name matches it's file path and file name.
:)
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 theApplication
class) the followin :Then your lib/ dir will be autoloaded.