I am creating a Rails 3 Engine and I added a few initializers to the engine class. 2 of them include modules in the main app’s classes at runtime. One includes some methods in the application_helper.rb and the other adds class methods to controllers using class_eval. It works fine in production mode because config.cache_classes=true. But in development, all the application classes are reloaded on every request, since development defaults to cache_classes=false. This was bugging me for a bit as I tried to find a clean way to reload the initializers on each request. After trying a few different things I came across a better solution. Remove the code from the initializer block and do this instead:
config.to_prepare do ActionView::Base.send(:include, MyEngineHelper) MyEngineControllerStuff.add_class_methods end
The config.to_prepare block is run once in production and on every request in development mode. So this only pertains to code that reopens your app’s classes. If you are doing something like including a module in ActiveRecord, you can still use an initializer b/c it will not reload the ActiveRecord class.