Search code examples
ruby-on-railsclassloaderactivesupport

Rails automatic class loading at custom directory


Rails has a feature where models, controllers, views, libraries, etc. are automatically loaded when they are needed. This is especially helpful in development mode, where they get automatically reloaded as well.

How do I tell Rails to perform automatic loading somewhere it doesn't expect to load files? Say, I create a folder app/addons or something, and I want it to load classes there in the same way models are loaded. So if I had app/addons/foo.rb, I want to be able to call the class Foo.


Solution

  • In your config/environment.rb add the following line to the Rails::Initializer.run block:

    config.load_paths += %W( #{RAILS_ROOT/app/addons} )