Wednesday, July 30, 2008

how/when resources are reloaded in Ruby/Rails

I'm developing a RoR application and wanted to keep the controller code as clean as possible by putting extra processing code in the /lib. As I made updates to the /lib resources, I noticed I had to keep restarting my server in order for changes to be picked up. No thank you! That's not why I moved to RoR.

One quick way to deal with this is to use 'require_dependency' instead of simply 'require'.

require 'foo'
require_dependency 'foo'

But when it comes time to migrate your code to production, you don't want the 'require_dependency' statement because it could really impact performance.

One solution seemed to suggest adding extra code in environments.rb
Dependencies.load_paths
Dependencies.explicitly_unloadable_constants

I haven't been able to get this to work successfully, so in the meantime, I will temporarily change 'require' to 'require_dependency' when I'm working with code in a library resource.

Another posting provided me with the 'require_dependency' option listed above (giving credit where credit is due).

No comments: