Adding a directory to the load path in Rails?
Ruby on-RailsRubyRuby on-Rails Problem Overview
As of Rails 2.3, what's the right way to add a directory to the load path so that it hooks into Rails' auto-reloading mechanisms?
The specific example I'm thinking of is I have a class that has several sub-classes using STI and I thought it would be a good idea to put them in a sub-directory rather than clutter the top-level. So I would have something like:
#app/models/widget.rb
class Widget < ActiveRecord::Base
add_to_load_path File.join(File.dirname(__FILE__), "widgets")
end
#app/models/widgets/bar_widget.rb
class BarWidget < Widget
end
#app/models/widgets/foo_widget.rb
class FooWidget < Widget
end
It's the add_to_load_path
method that I'm looking for.
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
In the current version of Rails (3.2.8), this has been changed in the application.rb file.
The code is currently commented out as:
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
Will need to update the autoload_paths value. Attempting to modify the the former load_paths variable causes this error.
/configuration.rb:85:in `method_missing': undefined method `load_paths' for #<Rails::Application::Configuration:0xac670b4> (NoMethodError)
for an example, for each path to add to autoload_paths config, add a line similar to the following:
config.autoload_paths += %W(#{config.root}/app/validators)
config.autoload_paths
accepts an array of paths from which Rails will autoload constants. Default is all directories under app
.
http://guides.rubyonrails.org/configuring.html
From commentor (hakunin) below:
If the directory is under app/
, you don't need to add it anywhere, it should just work by default (definitely in 3.2.12). Rails has eager_load_paths
that acts as autoload_paths
in development, and eager load in production. All app/*
directories are automatically added there.
Solution 2 - Ruby on-Rails
For older versions of Rails:
You can do this in your environment.rb config file.
config.load_paths << "#{RAILS_ROOT}/app/widgets"
--
For Rails 3, see answers bellow
Solution 3 - Ruby on-Rails
In Rails 5 you don't have to explicitly load folders from within the app directory anymore. All folders placed inside are directly available. You don't have to touch any of the config files. But it seems as if there are some issues with Spring.
The new workflow therefore is:
- create a new folder and class inside the /app directory
- run
spring stop
on the command line - check the autoload-paths with
bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'
on the command line. The new folder should now be listed. - run
spring start
on the command line
Solution 4 - Ruby on-Rails
In Rails 3, you can set this in config/application.rb, where this sample is provided by default:
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{config.root}/extras )
Solution 5 - Ruby on-Rails
On Rails 5 you need to add the following code to environment.rb:
# Add the widgets folder to the autoload path
Rails.application.configure do
config.autoload_paths << "#{Rails.root}/app/widgets"
end
Solution 6 - Ruby on-Rails
Another update for rails 3 -- activesupport 3.0.0:
Instead of:
ActiveSupport::Dependencies.load_paths << "#{RAILS_ROOT}/app/widgets"
You may need to do this:
ActiveSupport::Dependencies.autoload_paths << "#{RAILS_ROOT}/app/widgets"
Solution 7 - Ruby on-Rails
I found I needed to do this after config block-- no access to config object anymore.
This did the trick
ActiveSupport::Dependencies.load_paths << "#{RAILS_ROOT}/app/widgets"
Solution 8 - Ruby on-Rails
In config/application.rb
add config.autoload_paths << "#{config.root}/models/widgets"
.
File should look like this:
module MyApp
class Application < Rails::Application
config.autoload_paths << "#{config.root}/models/widgets"
end
end
I know this works for Rails 4 and 5. Probably others as well.
Solution 9 - Ruby on-Rails
If you want to add multiple directories:
config.autoload_paths += Dir[Rails.root / "components/*/app/public"]
(this is an example for packwerk autoload)