# Best way to enumerate STI subclasses

**URL:** <https://www.ruby-forum.com/t/best-way-to-enumerate-sti-subclasses/80693>\
**Category:** Rails\
**Created:** [December 30, 2006, 1:29am UTC](https://www.ruby-forum.com/t/best-way-to-enumerate-sti-subclasses/80693 "2006-12-30T01:29:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 30, 2006, 1:29am UTC](https://www.ruby-forum.com/t/best-way-to-enumerate-sti-subclasses/80693/1 "2006-12-30T01:29:51Z")

</div>

Hello,

Following the AWDWR STI example (People, Employee, Manager, etc). If I  
want to present a list of potential subclasses to add, and I plan on  
extending the list of subclasses over time, what is the best way to  
generate the subclass list?

For example, should I create a YAML file and parse that? Create a table  
in the db and read that in? Or is there some other way to get that list  
from ActiveRecord?

Thanks,  
Ryan G.

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 30, 2006, 11:49am UTC](https://www.ruby-forum.com/t/best-way-to-enumerate-sti-subclasses/80693/2 "2006-12-30T11:49:40Z")

</div>

Ryan G. wrote:

> from ActiveRecord?
> 
> Thanks,  
> Ryan G.

See this discussion:  
[http://groups.google.co.uk/group/rubyonrails-talk/browse\_thread/thread/475260e21167de9a/51721d1d1d9c9c50?lnk=st&q=inverse+base\_class&rnum=1&hl=en#51721d1d1d9c9c50](http://groups.google.co.uk/group/rubyonrails-talk/browse_thread/thread/475260e21167de9a/51721d1d1d9c9c50?lnk=st&q=inverse+base_class&rnum=1&hl=en#51721d1d1d9c9c50)

Alternatively you could just use a constant defined in the base class,  
something like  
SUBCLASSES = [Employee, Manager, Slave…]  
and then refer to it using Person::SUBCLASSES  
or perhaps prettier define a method in the base class  
def subclasses  
[Employee, Manager, Slave…]  
end

and then refer to it using Person.subclasses

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 30, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/best-way-to-enumerate-sti-subclasses/80693/3 "2006-12-30T23:25:36Z")

</div>

> Alternatively you could just use a constant defined in the base class,  
> something like  
> SUBCLASSES = [Employee, Manager, Slave…]  
> and then refer to it using Person::SUBCLASSES  
> or perhaps prettier define a method in the base class  
> def subclasses  
> [Employee, Manager, Slave…]  
> end
> 
> and then refer to it using Person.subclasses

OBJECT already supports sub\_classes method. you can use it on any model  
without defining hard-coded list.

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 31, 2006, 12:48am UTC](https://www.ruby-forum.com/t/best-way-to-enumerate-sti-subclasses/80693/4 "2006-12-31T00:48:53Z")

</div>

Elad M. - [Creopolis.com](http://Creopolis.com) wrote:

> > and then refer to it using Person.subclasses
> 
> OBJECT already supports sub\_classes method. you can use it on any model  
> without defining hard-coded list.
> 
> >

If you look at the referenced thread you’ll see there are some  
performance downsides to using Object#subclasses\_of, which I think is  
what you’re referring to…
