Here''s a simplistic model class. class Person < ActiveRecord::Base has_and_belongs_to_many :relatives, :class_name => ''Person'', :join_table => ''relatives'', :foreign_key => ''from_id'', :association_foreign_key => ''to_id'' has_and_belongs_to_many :brothers, :class_name => ''Person'', :join_table => ''relatives'', :foreign_key => ''from_id'', :association_foreign_key => ''to_id'', :conditions => "gender = ''m''" # ^ plain reference to column has_and_belongs_to_many :sisters, :class_name => ''Person'', :join_table => ''relatives'', :foreign_key => ''from_id'', :association_foreign_key => ''to_id'', :conditions => "sisters_people.gender = ''f''" # ^ qualified reference to column end The noteworthy things about it are that its habtm associations are self-referential and that two of them have conditions. Let''s see how to use this. Also, the brothers and sisters associations use different ways to refer to the column on which their respective condition depends. Lets put this class through its paces. >> Person.find(:first, :include => :sisters) -> works >> Person.find(:first, :include => :brothers) -> exception; invalid SQL, reference to column ''gender'' is ambiguous >> p = Person.find(:first) >> p.brothers.map(&:first_name) -> works >> p.sisters.map(&:first_name) -> exception; invalid SQL, no alias defined for sisters_people I may be missing something, but from what it looks like, ActiveRecord doesn''t handle this case as it should. Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Schuerig wrote:> Here''s a simplistic model class. > > class Person < ActiveRecord::Base > has_and_belongs_to_many :relatives, > :class_name => ''Person'', :join_table => ''relatives'', > :foreign_key => ''from_id'', :association_foreign_key => ''to_id'' > > > Michael >Could you please provide the schema that this code is running against. As a guess, it looks as though gender is defined in both the People? table and it''s join (Relatives?) which would lead to an ambiguous query unless it was explicitly qualified as it was in the sister''s case. ilan -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Schuerig wrote:> > The noteworthy things about it are that its habtm associations are > self-referential and that two of them have conditions. Let''s see how to > use this. Also, the brothers and sisters associations use different > ways to refer to the column on which their respective condition > depends.Hi Michael that''s exactly what I am trying to solve using a :through statement... if you solve this problem, please let me know... I''ll appreciate cloning your definitions.... Yves mailto:ydj.dufour-39ZsbGIQGT5GWvitb5QawA@public.gmane.org -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Schuerig
2007-Jan-03 17:54 UTC
Re: Self-referential habtm with condition is broken
On Wednesday 03 January 2007 16:35, Ilan Berci wrote:> Michael Schuerig wrote: > > Here''s a simplistic model class. > > > > class Person < ActiveRecord::Base > > has_and_belongs_to_many :relatives, > > > > :class_name => ''Person'', :join_table => ''relatives'', > > :foreign_key => ''from_id'', :association_foreign_key => > > : ''to_id''> Could you please provide the schema that this code is running > against. As a guess, it looks as though gender is defined in both the > People? table and it''s join (Relatives?) which would lead to an > ambiguous query unless it was explicitly qualified as it was in the > sister''s case.ActiveRecord::Schema.define(:version => 10) do create_table "people", :force => true do |t| t.column "first_name", :string, :limit => 40, :null => false t.column "last_name", :string, :limit => 40, :null => false t.column "gender", :string, :limit => 1, :null => end create_table "relatives", :id => false, :force => true do |t| t.column "from_id", :integer, :null => false t.column "to_id", :integer, :null => false end end In case it wasn''t clear from my original message, the point I''m trying to make is that apparently it is not possible to specify a condition on a habtm association in such a way that both fetching the associated objects and finding with the association :include''d are possible. Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---