I’m trying to join two tables with active record and not getting what I expect *surprise*. Two tables T1 and T2, both have a common key called ID. Given this relationship in the Models class T1 < ActiveRecord::Base has_one :T2 class T1 < ActiveRecord::Base belongs_to :T1 In the controller I have… @results = T1.all(:joins => :T2) @results only contains the columns from T1 and none from T2. If I do a find_by_sql as follows @results = T1.find_by_sql("select * from T1, T2 where T1.id = T2.id") @results contains the correct Cartesian product of T1, T2. Am I missing something in the active record call? Thanks in advance for your time... jeff... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2011-Jan-19 04:52 UTC
Re: Active Record Join does not return catesian product
Jeffrey H. wrote in post #975932:> I’m trying to join two tables with active record and not getting what I > expect *surprise*. > > Two tables T1 and T2, both have a common key called ID. Given this > relationship in the Models > > class T1 < ActiveRecord::Base > has_one :T2 > > class T1 < ActiveRecord::Base > belongs_to :T1 > > In the controller I have… > @results = T1.all(:joins => :T2) > @results only contains the columns from T1 and none from T2. > > If I do a find_by_sql as follows > @results = T1.find_by_sql("select * from T1, T2 where T1.id = T2.id") > @results contains the correct Cartesian product of T1, T2. > > Am I missing something in the active record call?No; rather, you''re missing something in the setup. Normally the foreign key for t2 would be t1.t2_id , not simply t1.id . I think your DB design has problems, frankly. You shouldn''t normally be trying to match the primary key in one table with the primary key in another. What are you trying to achieve here?> > Thanks in advance for your time... > jeff...Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Jan-19 10:59 UTC
Re: Active Record Join does not return catesian product
On Jan 19, 4:14 am, "Jeffrey H." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I’m trying to join two tables with active record and not getting what I > expect *surprise*. > > Two tables T1 and T2, both have a common key called ID. Given this > relationship in the Models > > class T1 < ActiveRecord::Base > has_one :T2 > > class T1 < ActiveRecord::Base > belongs_to :T1 > > In the controller I have… > @results = T1.all(:joins => :T2) > @results only contains the columns from T1 and none from T2. >The select clause defaults to t1.* If you want something else you have to ask for it Fred> If I do a find_by_sql as follows > @results = T1.find_by_sql("select * from T1, T2 where T1.id = T2.id") > @results contains the correct Cartesian product of T1, T2. > > Am I missing something in the active record call? > > Thanks in advance for your time... > jeff... > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.