Automatically Reload Modules On Change In Rails 2.0.x

This problem had me stumped longer than any other single problem I’ve run into so far, and no group or search query I could come up with was able to help me; ultimately I had to figure this out by diving into the actual Rails source code and digging until I found the right concoction. Perhaps this solution was not more findable because we’re running edge Rails? All data I could find on the forums seemed to say that Rails 1.2 did not have these sorts of problems, but I can neither accept nor refute that.

The problem I’d been having was that I had created a Rails Module in a file that was in a subdirectory of my /lib directory. The point of this Module was to allow me to share some methods between a select few Rails controllers without putting the methods into the global Application.rb (which seems sloppy to me…methods should only be available in the scope they’re needed says I). Actually, figuring out that I could create a Module in the lib directory to DRY up my controllers was also surprisingly difficult to find Google information on… you’d think people would object to putting stuff in Application.rb more often. Anyway, I digress.

This Module in the lib subdirectory had one annoying problem: when I made changes to it, I had to stop and re-start my Rails server for those changes to take effect. That got old fast. After the aforementioned digging, here were the TWO LINES OF CODE that took me way too many hours to figure out.

In Environment.rb: Add the path for the lib subdirectory where my module was stored, e.g.

config.load_paths += %W( #{RAILS_ROOT}/lib/item_setup )

There is a commented line in environment that has an example of the syntax for this.

In Development.rb: Add the name of my Module to the explicitly_unloadable_constants list, e.g.,

Dependencies.explicitly_unloadable_constants = 'NameOfMyModule'

And voila! It works. If you have other modules you want to automatically reload, use the << operator to add them to the explicitly_unloadable_constants array, e.g.,

Dependencies.explicitly_unloadable_constants << 'NameOfAnotherModule'

The other possible problem that I read about on the forums that didn’t come about for me was that if my lib directory had somehow gotten listed in Dependencies.load_once_paths, then it would not reload on being changed. You can Google around to figure out how to fix that (hint: delete your directory from the load_once_paths array), but it probably won’t be an issue.

5 Replies to “Automatically Reload Modules On Change In Rails 2.0.x”

  1. I totally understand your frustration. I’ve run into this problem myself on more than one occasion.
    Have you ever heard that some of your best ideas come in the middle of the night? You should always keep a pencil and paper on your nightstand.
    I woke up one morning, and tho I don’t even remember writing it, there it was on the notepad:

    In Development.rb: Add the name of my Module to the explicitly_unloadable_constants list, e.g., €œDependencies.explicitly_unloadable_constants = €˜ItemSetupUtil €™ €

    If you would’ve called me I could have told you and saved you a lot of trouble.

    Love always, mom

  2. Such a great post ! Exactly what I was looking for.

    Apparently, Rails developers are used to sharing code between their controllers by feeding application.rb. When you understand the power of Ruby mixins, you suddenly see you’ve got other options. At least, if you’re able to get seamless reloads in dev mode. That’s where your post comes in.

Leave a Reply to pishposh59 Cancel reply

Your email address will not be published. Required fields are marked *