[Rails] Single Table inheitance doesn't show subclasses ?

Scott Willson scott at butlerpress.com
Wed Jan 4 20:59:43 GMT 2006


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

> Suppose you have:
>
> ---
> class Animal < ActiveRecord::Base
> end
>
> class Mammal < Animal
> end
>
> class Cow < Mammal
> end
> ---
>
> 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.

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

Scott


More information about the Rails mailing list