Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a directory to the load path in Rails?

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.

like image 978
pjb3 Avatar asked Aug 03 '09 17:08

pjb3


People also ask

What is autoloading in Rails?

Rails automatically reloads classes and modules if application files in the autoload paths change. More precisely, if the web server is running and application files have been modified, Rails unloads all autoloaded constants managed by the main autoloader just before the next request is processed.

What is load path in Ruby?

Ruby by default has a list of directories it can look through when you ask it to load a specific file. This is stored in a variable: $: This is the load path. It initially includes the libdir, archdir, sitedir, vendordir and some others and is information Ruby holds about itself. If you type ruby -e 'puts $LOAD_PATH'

What is autoload in Ruby?

Ruby has an in-built module autoload, which comes into action whenever a specific module or a class is accessed or called upon from the parent or calling class or module. Upon receiving a call, this module registers the corresponding file path to the called module.


1 Answers

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.

like image 145
Jamel Toms Avatar answered Sep 23 '22 11:09

Jamel Toms