caching some classes, not others

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

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

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!

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

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

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

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.

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

I in turn wonder if Person.reset_subclasses would also work.

///ark