Rails class cacheing in development mode


#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

On Mar 30, 6:20 pm, sweet61 removed_email_address@domain.invalid wrote:

It doesn’t rerun any initialization. If you add your file to
ActiveSupport::Dependencies.load_once_paths then it won’t get
unloaded.

Fred


#3

Thanks Frederick,

That worked perfectly. I modified environment.rb as follows :-

config.load_paths += %W( #{RAILS_ROOT}/lib/soap_clients/company #
{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)

On Mar 30, 6:24 pm, Frederick C. removed_email_address@domain.invalid


#4

Sorry Frederick. This issue is partly resolved but not quite.

The problem I think is that I am patching ActiveRecord during
initialization. I have stopped my validation code from being unloaded,
however when activerecord is reloaded, or particularly my models then
they are no longer patched. Does that sound reasonable?

Is there some way I can patch ActiveRecord so it will be patched again
when the class is reloaded?

Of course I may be totally off-base here and the problem is down to
the way ActionWebService is reloading classes, as the model classes
are being used within ActionWebService and it is this that is throwing
the exception.

Steve


#5

On Mar 30, 9:00 pm, sweet61 removed_email_address@domain.invalid wrote:

Sorry Frederick. This issue is partly resolved but not quite.

The problem I think is that I am patching ActiveRecord during
initialization. I have stopped my validation code from being unloaded,
however when activerecord is reloaded, or particularly my models then
they are no longer patched. Does that sound reasonable?

Is there some way I can patch ActiveRecord so it will be patched again
when the class is reloaded?

ActiveRecord isn’t reloaded ever - only your code is reloaded (which
is problematic in your initial case because). Unless you were doing
something particularly funky I’m not sure off the top of my head what
would be wrong.

Of course I may be totally off-base here and the problem is down to
the way ActionWebService is reloading classes, as the model classes
are being used within ActionWebService and it is this that is throwing
the exception.

Don’t think that has anything to do with that - the reloading stuff is
just part of the general request lifecycle stufff.

Fred


#6

When you say, “Unless I’m doing something particularly funky”,
well…

I have a hand-rolled validator that validates my models based upon the
company that is submitting the data and the country they are in.

This validator internally uses it’s own active-record models to hold
the validation data, so it has models called Model, Field, FieldItems
etc. I now understand that ActiveRecord is never reloaded, however my
validation models are presumably unloaded. This leaves my validation
module loaded but it’s internally used models are unloaded.

Does this explain the behaviour I am seeing and is there a way around
it?

On Mar 30, 9:37 pm, Frederick C. removed_email_address@domain.invalid