Rails 3.2 dev mode code reloading and the lib directory.
24 Mar 2012, by Ian Leitch

Just a quick tip.

If you’re using Rails 3.2 and the file change based code reloading feature but don’t add lib to autoload_paths, then changes to files in lib are not noticed when you refresh your browser.

Many people probably just add lib to autoload_paths, but this is not the way the Rails core team intends for lib to be used. The lib directory is where the vast majority of your app should live, all your domain logic should go there. As with other external dependencies (and Rails is an external dependency of your app), you need to require code manually.

The recommended approach is to leave lib out of autoload_paths and require files as needed using require_dependency.

The magic source to your files in lib reloaded when changed, is:

config.watchable_dirs['lib'] = [:rb]

There’s also config.watchable_files if you just want to watch individual files.

blog comments powered by Disqus