I''m having difficulty with single table inheritance when the STI table is the "belongs_to" in a has_many / belongs_to relationship. Here''s what the models look like (contrived and simplified) : pie_lover.rb class PieLover < ActiveRecord::Base has_many :pies end pie.rb class Pie < ActiveRecord::Base belongs_to :pie_lover end cherry_pie.rb class CherryPie < Pie end apple_pie.rb class ApplePie < Pie end Can someone help me understand why: This works: CherryPie.count And this works: PieLover.find(:first).pies.count But this fails: PieLover.find(:first).cherry_pies.count I tried adding "belongs_to :pie_lover" in cherry_pie.rb but it didn''t make a difference. I''m guessing that I''m missing something simple. Thanks for the guidance, folks! - Don -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Oct 11, 10:48 pm, Don Stocks <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m having difficulty with single table inheritance when the STI table > is the "belongs_to" in a has_many / belongs_to relationship. Here''s > what the models look like (contrived and simplified) : > > pie_lover.rb > class PieLover < ActiveRecord::Base > has_many :pies > end > > pie.rb > class Pie < ActiveRecord::Base > belongs_to :pie_lover > end > > cherry_pie.rb > class CherryPie < Pie > end > > apple_pie.rb > class ApplePie < Pie > end > > Can someone help me understand why: > > This works: > CherryPie.count > > And this works: > PieLover.find(:first).pies.count > > But this fails: > PieLover.find(:first).cherry_pies.count > > I tried adding "belongs_to :pie_lover" in cherry_pie.rb but it didn''t > make a difference. I''m guessing that I''m missing something simple. > > Thanks for the guidance, folks! > > - Don > > -- > Posted viahttp://www.ruby-forum.com/.If you want to find all pies, then ''PieLover.find(:first).pies.count'' If you want just CherryPies, then PieLover has to ''has_many :cherry_pies''. _Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Don Stocks
2006-Oct-12 03:33 UTC
Re: STI Troubles in a has_many : belongs_to relationship.
> > I tried adding "belongs_to :pie_lover" in cherry_pie.rb but it didn''t > make a difference. I''m guessing that I''m missing something simple. >OK, it was something simple. I didn''t add "has_many :cherry_pies" to pie_lover.rb. Duh! Since the STI section of the Agile Rails book doesn''t mention STI and relationships, I assumed that you could get away with declaring the relationship at the base of the object model. I didn''t realize that I needed to declare each subclass explicitly. But since that''s how everything else works.... guess it''s time for a break! Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Don Stocks
2006-Oct-12 03:35 UTC
Re: STI Troubles in a has_many : belongs_to relationship.
> > If you want just CherryPies, then PieLover has to ''has_many > :cherry_pies''.Thanks Kevin! I did figured it out as you were typing. ;) Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---