Adding a directory to the boot path in Rails?

As with Rails 2.3, what is the right way to add a directory to the boot path so that it connects to Rails's automatic reload mechanisms?

In the specific example that I think of, I have a class that has several subclasses using STI, and I thought it would be nice to put them in a subdirectory rather than cluttering 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 

This is the add_to_load_path method I'm looking for.

+57
ruby ruby-on-rails
Aug 03 '09 at 17:11
source share
8 answers

For older versions of Rails:

You can do this in the environment.rb configuration file.

 config.load_paths << "#{RAILS_ROOT}/app/widgets" 

-

For Rails 3 , see below for answers

+60
Aug 03 '09 at 17:16
source share

In the current version of Rails (3.2.8), this has been changed in the application.rb file.

The code is currently commented as:

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

You must update the autoload_paths value. Attempting to modify the old variable load_paths causes this error.

 /configuration.rb:85:in `method_missing': undefined method `load_paths' for #<Rails::Application::Configuration:0xac670b4> (NoMethodError) 

for example, for each path to add autoload_paths to the configuration, 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 auto-configure constants. By default, all directories are under the app .

http://guides.rubyonrails.org/configuring.html




From the commentator (hakunin) below:

If the directory is under app/ , you do not need to add it anywhere, it should work only by default (definitely in 3.2.12). Rails has eager_load_paths , which acts like autoload_paths during development and has a high load. All app/* directories are automatically added here.

+100
Oct 10 '12 at 18:44
source share

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 ) 
+27
Sep 06 '10 at 16:51
source share

Another update for rails 3 is activesupport 3.0.0:

Instead:

 ActiveSupport::Dependencies.load_paths << "#{RAILS_ROOT}/app/widgets" 

You may need the following:

 ActiveSupport::Dependencies.autoload_paths << "#{RAILS_ROOT}/app/widgets" 
+8
Oct 12 2018-10-12T00:
source share

In 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 
+7
Sep 03 '16 at 21:44
source share

In Rails 5, you no longer need to explicitly load folders from the application directory. All folders located inside are directly accessible. You do not need to touch any configuration files. But it seems that there are problems with Spring.

So the new workflow:

  • create a new folder and class inside the / app directory
  • run spring stop on the command line
  • check startup paths using bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths' on the command line. A new folder should now be specified.
  • run spring start at the command prompt
+5
Feb 15 '17 at 17:01
source share

In config/application.rb add config.autoload_path << "#{config.root}/dir/widgets"

The file should look like this:

 module MyApp class Application < Rails::Application config.autoload_paths << "config.root}/widgets" end end 

I know this works for Rails 4 and 5. Perhaps others.

+2
Oct 10 '17 at 20:33
source share

I found that I need to do this after the configuration block - there is no longer access to the configuration object.

It did the trick

 ActiveSupport::Dependencies.load_paths << "#{RAILS_ROOT}/app/widgets" 
+1
Sep 26 '09 at 21:30
source share



All Articles