Best way to enumerate STI subclasses

Ryan Glover wrote:

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 Glover

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

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

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.

Elad Meidar - Creopolis.com wrote:

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.

>

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