Object.subclasses_of(MyModel) not working in development environment

I’m trying to use the subclasses_of method but I’m getting strange
results.

The first request after I restart the server, everything works fine.
Subsequent
requests return an empty array.

Can anyone shed any light on why this happens or what I can do about it?

Thanks,
Gareth

On 21 Feb 2008, at 11:34, Gareth A. wrote:

I’m trying to use the subclasses_of method but I’m getting strange
results.

The first request after I restart the server, everything works fine.
Subsequent
requests return an empty array.

Can anyone shed any light on why this happens or what I can do about
it?

This is probably to do with rails’ magic unloading of your classes
after the request has run. Be aware that subclasses_of might not give
you all your subclasses, because if the file hasn’t been loaded yet
then subclasses_of won’t know about it.

Fred

Frederick C. <frederick.cheung@…> writes:

On 21 Feb 2008, at 11:34, Gareth A. wrote:

Can anyone shed any light on why this happens or what I can do about
it?

This is probably to do with rails’ magic unloading of your classes
after the request has run. Be aware that subclasses_of might not give
you all your subclasses, because if the file hasn’t been loaded yet
then subclasses_of won’t know about it.

Thanks, Fred,

I’m aware that not all models are loaded by default and I have a
Dir[…].each
{|f| require f} to make sure the relevant subclasses are loaded when I
need to
access them.

I did know that Rails had some magic to let you see your updated models
without
having to restart your server every request, but I’m still confused why
Ruby
doesn’t see these classes in subsequent requests. If they’re unloaded at
the end
of the request then surely they get reloaded the next time I need them

that’s the point, isn’t it?

Gareth

On 21 Feb 2008, at 12:19, Gareth A. wrote:

you all your subclasses, because if the file hasn’t been loaded yet
I did know that Rails had some magic to let you see your updated
models without
having to restart your server every request, but I’m still confused
why Ruby
doesn’t see these classes in subsequent requests. If they’re
unloaded at the end
of the request then surely they get reloaded the next time I need
them -
that’s the point, isn’t it?

One thing that does happen is that when rails unloads stuff, it
essentially empties out existing classes. If you’re somehow hanging
onto an old reference to MyModel and call subclasses_of on that then
it’s possible that you’d get back an empty array

Fred