# Object.subclasses\_of(MyModel) not working in development environment

**URL:** <https://www.ruby-forum.com/t/object-subclasses-of-mymodel-not-working-in-development-environment/129775>\
**Category:** Rails\
**Created:** [February 21, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/object-subclasses-of-mymodel-not-working-in-development-environment/129775 "2008-02-21T12:35:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [February 21, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/object-subclasses-of-mymodel-not-working-in-development-environment/129775/1 "2008-02-21T12:35:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [February 21, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/object-subclasses-of-mymodel-not-working-in-development-environment/129775/2 "2008-02-21T12:54:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [February 21, 2008, 1:21pm UTC](https://www.ruby-forum.com/t/object-subclasses-of-mymodel-not-working-in-development-environment/129775/3 "2008-02-21T13:21:03Z")

</div>

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 _re_loaded the next time I need them

that’s the point, isn’t it?

Gareth

---

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [February 21, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/object-subclasses-of-mymodel-not-working-in-development-environment/129775/4 "2008-02-21T13:55:04Z")

</div>

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 _re_loaded 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
