Hello I have write a code to select data from two tables (T1,T2) but the problem is @tables= T1.find_by_sql("select T1.*,T2.* from T1inner join T2on T1.followup_id = T2.id where followups.userid = ''41'' ") THe problem all values that is returned from running only T1 variables,why? Where are T2 variables?? -- 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 -~----------~----~----~----~------~----~------~--~---
On Jun 26, 10:08 pm, Mohamed Saeed <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello > > I have write a code to select data from two tables (T1,T2) but the > problem is > > @tables= T1.find_by_sql("select T1.*,T2.* from T1inner join T2on > T1.followup_id = T2.id where followups.userid = ''41'' ") > > THe problem all values that is returned from running only T1 > variables,why? > Where are T2 variables??That''s what find_by_sql does: you''ll always get and instance of T1 (with some extra attributes though, can''t remember if the console will show them by default. Also be careful doing what you;ve done because you could easily have overwritten the id attribute with the one from T2, which will cause havoc when you try and save the objects you''ve loaded. Lastly with the write associations you can probably do without the call to find_by_sql. Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Jun 26, 10:08�pm, Mohamed Saeed <rails-mailing-l...@andreas-s.net> > wrote: >> Where are T2 variables?? > That''s what find_by_sql does: you''ll always get and instance of T1 > (with some extra attributes though, can''t remember if the console will > show them by default. Also be careful doing what you;ve done because > you could easily have overwritten the id attribute with the one from > T2, which will cause havoc when you try and save the objects you''ve > loaded. Lastly with the write associations you can probably do without > the call to find_by_sql. > > Fredtrue, but I believe I''m correct in saying that if you did "select T1.*, T2.name as t2_name ..." it would correctly load the "t2_name" as an attribute of the T1 object. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 27 Jun 2008, at 09:40, Matthew Rudy Jacobs wrote:> > Frederick Cheung wrote: >> On Jun 26, 10:08�pm, Mohamed Saeed <rails-mailing-l...@andreas- >> s.net> >> wrote: >>> Where are T2 variables?? >> That''s what find_by_sql does: you''ll always get and instance of T1 >> (with some extra attributes though, can''t remember if the console >> will >> show them by default. Also be careful doing what you;ve done because >> you could easily have overwritten the id attribute with the one from >> T2, which will cause havoc when you try and save the objects you''ve >> loaded. Lastly with the write associations you can probably do >> without >> the call to find_by_sql. >> >> Fred > > true, > but I believe I''m correct in saying that if you did > > "select T1.*, T2.name as t2_name ..." > it would correctly load the "t2_name" as an attribute of the T1 > object.Yup that should work. You just won''t see t2_name if you''re messing around in the console (except of course if you call attributes or t2_name): the default output format in rails 2 just shows attributes from the main table. Fred> > > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 27 Jun 2008, at 09:40, Matthew Rudy Jacobs wrote: > >>> you could easily have overwritten the id attribute with the one from >> "select T1.*, T2.name as t2_name ..." >> it would correctly load the "t2_name" as an attribute of the T1 >> object. > > Yup that should work. You just won''t see t2_name if you''re messing > around in the console (except of course if you call attributes or > t2_name): the default output format in rails 2 just shows attributes > from the main table. > > Fredyes your are right and i am tottaly accept but when i did that it throws exeption "Attribute doesn''t exist in array..." -- 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 -~----------~----~----~----~------~----~------~--~---
On 27 Jun 2008, at 10:39, Mohamed Saeed wrote:> > Frederick Cheung wrote: >> On 27 Jun 2008, at 09:40, Matthew Rudy Jacobs wrote: >> >>>> you could easily have overwritten the id attribute with the one >>>> from >>> "select T1.*, T2.name as t2_name ..." >>> it would correctly load the "t2_name" as an attribute of the T1 >>> object. >> >> Yup that should work. You just won''t see t2_name if you''re messing >> around in the console (except of course if you call attributes or >> t2_name): the default output format in rails 2 just shows attributes >> from the main table. >> >> Fred > > yes your are right and i am tottaly accept but when i did that it > throwsdid what? Fred> > exeption "Attribute doesn''t exist in array..." > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 27 Jun 2008, at 10:39, Mohamed Saeed wrote: > >>> Yup that should work. You just won''t see t2_name if you''re messing >>> around in the console (except of course if you call attributes or >>> t2_name): the default output format in rails 2 just shows attributes >>> from the main table. >>> >>> Fred >> >> yes your are right and i am tottaly accept but when i did that it >> throws > > did what? > > Fred@tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on T1.followup_id = T2.id where followups.userid = ''41'' ") puts @tables[:t2_name] -- 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 -~----------~----~----~----~------~----~------~--~---
On 27 Jun 2008, at 10:58, Mohamed Saeed wrote:> > Frederick Cheung wrote: >> On 27 Jun 2008, at 10:39, Mohamed Saeed wrote: >> >>>> Yup that should work. You just won''t see t2_name if you''re messing >>>> around in the console (except of course if you call attributes or >>>> t2_name): the default output format in rails 2 just shows >>>> attributes >>>> from the main table. >>>> >>>> Fred >>> >>> yes your are right and i am tottaly accept but when i did that it >>> throws >> >> did what? >> >> Fred > > @tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on > T1.followup_id = T2.id where followups.userid = ''41'' ") > puts @tables[:t2_name]@tables is an array. You''d have to do something like @tables.first[:foo] Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Mohamed Saeed wrote:> > @tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on > T1.followup_id = T2.id where followups.userid = ''41'' ") > puts @tables[:t2_name]and you missed out the "T2.name as t2_name";>> @tables= T1.find_by_sql("select T1.*,T2.name AS t2_name from T1 inner join T2 on T1.followup_id = T2.id where followups.userid = ''41'' ") >> puts @tables.first[:t2_name]-- 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs wrote:> Mohamed Saeed wrote: > >> >> @tables= T1.find_by_sql("select T1.*,T2.* from T1 inner join T2on >> T1.followup_id = T2.id where followups.userid = ''41'' ") >> puts @tables[:t2_name] > > and you missed out the "T2.name as t2_name"; > > >>> @tables= T1.find_by_sql("select T1.*,T2.name AS t2_name from T1 inner join T2 on T1.followup_id = T2.id where followups.userid = ''41'' ") >>> puts @tables.first[:t2_name]Thanks Fred Now it''s working the problem was in @tables.first[:t2_name], also you are right console display only the attributes of the table that i used in finding "T1" Thanks Mohamed -- 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 -~----------~----~----~----~------~----~------~--~---