views:

142

answers:

1

I have a rails application that patches ActiveRecord with a hand-coded validator.

The patch is made by adding the following lines in config/environment.rb

Rails::Initializer.run do |config|  
...    
end  

class ActiveRecord::Base  
  include MtLib::DBValidations  
end

This works fine in production mode i.e. with

config.cache_classes = true

however it does not work in development with cache_classes set to false.

The error thrown is

    ArgumentError (A copy of MtLib::DBValidations has been removed from  
      the module tree but is still active!):

My question is what is the process that is followed when cache_class is set to false. Does Rails re-run any of the initialization methods? If not then where is the best place for me to put my patch to ensure that it is in all models and survives a classes reload?

I have tried adding the patch to config/initializers/active_record_patch, however this is not re-run when the classes are reloaded.

+2  A: 

The solution to this, provided by Frederick Cheung on the Ruby On Rails google group add the directory containing the loaded class into the load_once_path array.

I edited environment.rb to look like this

 config.load_paths +=  
   %W( #{RAILS_ROOT}/lib/soap_clients/carefone #{RAILS_ROOT}/lib/mt_lib)  

  # Make sure load_once_paths is a subset of load_paths  
  config.load_once_paths +=  %W( #{RAILS_ROOT}/lib/mt_lib)

And now this works in development mode without having to reload the server on every request

Steve Weet