# Caching some classes, not others

**URL:** <https://www.ruby-forum.com/t/caching-some-classes-not-others/124846>\
**Category:** Rails\
**Created:** [January 3, 2008, 2:38am UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846 "2008-01-03T02:38:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 2:38am UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/1 "2008-01-03T02:38:31Z")

</div>

Hi all,

I’ve got an issue that came up once I went to two production servers  
with a class (a set of classes actually) that gets updated  
dynamically. It only gets updated on one server and not the other  
until I do a restart (which is the correct rails behavior, but not  
what I want).

It looks to me like the Dependencies Module does a “require” if you  
choose to cache\_classes, and a “load” otherwise, and they get called  
each time the class is referenced. Or something like that.

Anyway, I have a set of classes that are created and updated on the  
fly, for which the underlying tables are created and updated on the  
fly, and for those I’d like to re-load the class each time it is  
referenced (or re-require it). Actually, ideally I’d only do this  
when the class/table definition is changed, because those moments are  
easily identified in my code. The problem with that of course is that  
notifying the other server(s) that HEY, UPDATE YOUR MODEL is non-  
trivial (to me anyway), so I’d be happy with a solution that just did  
a load for that set of classes as though they were running in  
development mode, while the rest of my classes were cached normally.

Any help on this is greatly appreciated.

Thanks,  
Brad

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 2:48am UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/2 "2008-01-03T02:48:26Z")

</div>

Hey, Brad. You can do something like:

```
    load "#{RAILS_ROOT}/app/models/person.rb"

```

in your code when you want to re-load the model. Not sure if this will  
do  
exactly what you want, but it is something to try.

HTH,

Jamey

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 5:28am UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/3 "2008-01-03T05:28:02Z")

</div>

That is very embarrassing for me. Thanks Jamey. I had convinced  
myself that once you "require"d something, you couldn’t then do a  
“load”. Next time, test first, ask questions later!

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/4 "2008-01-03T16:36:44Z")

</div>

More embarrassing is the fact that this doesn’t actually solve my  
problem…

What I really need is a way to force ActiveRecord to reload the  
columns on a particular class, as though that class were running in  
dev mode. (I’m on Rails 1.2.3 btw). As I mentioned above, I create  
the classes on the fly, but simply re-defining them each time doesn’t  
trigger AR to re-query the DB.

I’m digging into the code, but if anyone has any idea of how to force  
this to happen for a model class, I’d appreciate it.

Brad

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/5 "2008-01-03T17:40:02Z")

</div>

On 3 Jan 2008, at 16:22, Jamey C. wrote:

> I remember seeing something similar while googling a few days ago  
> for another issue.
> 
> I wonder if you did something like:  
> remove\_const(‘Person’)  
> load “#{RAILS\_ROOT}/app/models/person.rb”
> 
> If that would cause Rails to re-query the db for the table’s  
> columns.

I think that might screw things up (eg if other things have references  
to the Person class, eg via associations)  
You can do Person.reset\_column\_information after you’ve newly defined  
person.

Fred

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/6 "2008-01-03T17:22:52Z")

</div>

I remember seeing something similar while googling a few days ago for  
another issue.

I wonder if you did something like:  
remove\_const(‘Person’)  
load “#{RAILS\_ROOT}/app/models/person.rb”

If that would cause Rails to re-query the db for the table’s columns.

Jamey

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/7 "2008-01-03T18:00:38Z")

</div>

Yeah, that’s definitely better. I was doing things my way because I  
needed  
to have some class level macros re-execute, but if Brad just needs the  
table  
structure re-queried, reset\_column\_information looks like just the  
ticket.

On Jan 3, 2008 11:39 AM, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/8 "2008-01-03T18:44:15Z")

</div>

Fred, thank you, reset\_column\_information looks like it’s working. I  
was scanning base.rb for a solution and apparently I glazed over  
before I got to that method.

I was also trying your approach Jamey, of removing and redefining the  
constant and was having some issues.

Thanks a lot for the responses!

Brad

---

<div class="post-metadata">

**Author:** ![bbennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c0e974/32.png) [@bbennett](https://www.ruby-forum.com/u/bbennett)\
**Post date:** [January 3, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/caching-some-classes-not-others/124846/9 "2008-01-03T20:29:42Z")

</div>

On Jan 3, 8:22 am, “Jamey C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I wonder if you did something like:  
> remove\_const(‘Person’)  
> load “#{RAILS\_ROOT}/app/models/person.rb”

I in turn wonder if Person.reset\_subclasses would also work.

///ark
