Marcus Ob
2006-Jan-01 18:40 UTC
[Rails] Single Table inheitance doesn''t show subclasses ?
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) -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jan-02 11:33 UTC
[Rails] Re: Single Table inheitance doesn''t show subclasses ?
Marcus Ob wrote:> 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?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.
Marcus Ob
2006-Jan-02 12:22 UTC
[Rails] Re: Single Table inheitance doesn''t show subclasses ?
Hi Mark, Thank you for the example! Best, Marcus. -- Posted via http://www.ruby-forum.com/.
Scott Willson
2006-Jan-04 20:59 UTC
[Rails] Single Table inheitance doesn''t show subclasses ?
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