pjb3 pjb3 - 6 months ago 26
Ruby Question

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:

class Widget < ActiveRecord::Base
add_to_load_path File.join(File.dirname(__FILE__), "widgets")

class BarWidget < Widget

class FooWidget < Widget

It's the
method that I'm looking for.


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