Forum: Ruby on Rails Rails class cacheing in development mode

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Steve W. (Guest)
on 2009-03-30 21:23
(Received via mailing list)
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.
Frederick C. (Guest)
on 2009-03-30 21:25
(Received via mailing list)
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
Steve W. (Guest)
on 2009-03-30 22:13
(Received via mailing list)
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>
Steve W. (Guest)
on 2009-03-31 00:01
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-03-31 00:38
(Received via mailing list)
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
Steve W. (Guest)
on 2009-03-31 02:33
(Received via mailing list)
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>
This topic is locked and can not be replied to.