# Rails class cacheing in development mode

**URL:** <https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273>\
**Category:** Rails\
**Created:** [March 30, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273 "2009-03-30T19:23:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 30, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273/1 "2009-03-30T19:23:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 30, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273/2 "2009-03-30T19:25:44Z")

</div>

On Mar 30, 6:20 pm, sweet61 [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 30, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273/3 "2009-03-30T20:13:49Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 30, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273/4 "2009-03-30T22:01:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 30, 2009, 10:38pm UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273/5 "2009-03-30T22:38:09Z")

</div>

On Mar 30, 9:00 pm, sweet61 [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 31, 2009, 12:33am UTC](https://www.ruby-forum.com/t/rails-class-cacheing-in-development-mode/164273/6 "2009-03-31T00:33:34Z")

</div>

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](mailto:removed_email_address@domain.invalid)
