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
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 Cheung <frederick.cheung@...> writes:
> 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 *re*loaded the next time I need them -
that's the point, isn't it?
Gareth
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