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.