# Single Table inheitance doesn't show subclasses?

**URL:** <https://www.ruby-forum.com/t/single-table-inheitance-doesnt-show-subclasses/51495>\
**Category:** Rails\
**Created:** [January 1, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/single-table-inheitance-doesnt-show-subclasses/51495 "2006-01-01T19:40:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrqzzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mrqzzz](https://www.ruby-forum.com/u/mrqzzz)\
**Post date:** [January 1, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/single-table-inheitance-doesnt-show-subclasses/51495/1 "2006-01-01T19:40:27Z")

</div>

Hi, I am a Rails noob.

Suppose you have:

* * *

class Animal \< ActiveRecord::Base  
end

class Mammal \< Animal  
end

## class Cow \< Mammal end

I provide Animal,Mammal and Cow controllers with the scaffolding, just  
to test and to fill up the Animal table. I can see the “type” column  
getting it’s value set with the class name.  
So far, soo good.

Now, since listing Animals is showing all animals in the table  
(including Mammals and Cows) i thought that listing Mammals would show  
me all Mammals _and_ Cows, but i see only recods marked “Mammal” in the  
“type” column (and no Cows).

I understand that Rails doesn’t select descendants of a class when  
listing it’s elements, and i understand it could be more practical that  
way in most cases.

But how can i tell Rails to build the query including in the selection  
also any descendants?

thanks,  
Happy new year,  
Marcus.  
(sorry for my poor english)

---

<div class="post-metadata">

**Author:** ![mrqzzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mrqzzz](https://www.ruby-forum.com/u/mrqzzz)\
**Post date:** [January 2, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/single-table-inheitance-doesnt-show-subclasses/51495/2 "2006-01-02T12:33:41Z")

</div>

Marcus Ob wrote:

> end  
> “type” column (and no Cows).
> 
> I understand that Rails doesn’t select descendants of a class when  
> listing it’s elements, and i understand it could be more practical that  
> way in most cases.
> 
> But how can i tell Rails to build the query including in the selection  
> also any descendants?

Perhaps something like this would work Marcus.  
(Completely untested, may not work.)

class Animal \< ActiveRecord::Base  
@@branch = “’#{self.class.to\_s}’”  
def self.add\_descendent( klass )  
@@branch.concat( “,’#{klass.to\_s}’” )  
super unless self.equal?(Animal)  
end  
end

Put this inside all subclasses of Animal

```
@@branch = "'#{self.class.to_s}'"
superclass.add_descendent(self)

```

then use this SQL fragment to select objects of this class and  
all subclasses:

```
             "where type in (#@@branch)"

```

Or just create the branch string manually for each class.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![mrqzzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mrqzzz](https://www.ruby-forum.com/u/mrqzzz)\
**Post date:** [January 2, 2006, 1:22pm UTC](https://www.ruby-forum.com/t/single-table-inheitance-doesnt-show-subclasses/51495/3 "2006-01-02T13:22:22Z")

</div>

Hi Mark,  
Thank you for the example!

Best,  
Marcus.

---

<div class="post-metadata">

**Author:** ![mrqzzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mrqzzz](https://www.ruby-forum.com/u/mrqzzz)\
**Post date:** [January 4, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/single-table-inheitance-doesnt-show-subclasses/51495/4 "2006-01-04T22:00:59Z")

</div>

On Jan 1, 2006, at 10:40 AM, Marcus Ob wrote:

> end  
> that  
> way in most cases.

Actually, Rails _does_ select Mammals and Cows, but only if the  
current Ruby interpreter knows about Cow (i.e., has loaded the Cow  
class). The simplest workaround is to make sure that you reference  
Cow before running your query.

There’s an example with a test case here: [http://butlerpress.com/sti.zip](http://butlerpress.com/sti.zip)

Scott
