Ad Ber
2011-Apr-11 07:11 UTC
Display specific field that has duplicate name to other table
HI TO ALL I want to display a specific field in my VIEW but they have the same FIELD name..For example, th id in the PAYMENTS table and the id in the SUGGESTS table have the same field name... I want to be able to display the correct PAYMENTS id and correct SUGGESTS id.. My Database is big im having a hard time to trace..which id it belongs to. In my ruby on rails action or method Controller: def print_two_table @p = Pay.find_by_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM pays as P LEFT OUTER JOIN suggests AS s ON s.b_id = p.b_id WHERE p.created_at BETWEEN ''2008-03-04'' AND ''2008-07-06'' ") end In my View action or method: <%= @p.each do |form| %> <%= form.income %><br /> <%= form.id %> <br /> <%= form.income %><br /> <% end %> It gives me the wrong id i want to display..I want to display id in payments. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-11 07:17 UTC
Re: Display specific field that has duplicate name to other table
On Apr 11, 8:11 am, Ad Ber <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> HI TO ALL > > I want to display a specific field in > my VIEW but they have the same FIELD name..For example, th id in the > PAYMENTS table and the id in the SUGGESTS table have the same field > name... I want to be able to display the correct PAYMENTS id and > correct SUGGESTS id.. > My Database is big im having a hard time to trace..which id it belongs > to. > > In my ruby on rails action or method Controller: > def print_two_table > @p = Pay.find_by_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM > pays as P LEFT OUTER JOIN suggests AS s ON s.b_id = p.b_id WHERE > p.created_at BETWEEN ''2008-03-04'' AND ''2008-07-06'' ") > endIf you''re going to do it like this (rather than using associations) you''re going to need to alias the column names (ie select s.id as suggest_id) Fred> > In my View action or method: > > <%= @p.each do |form| %> > <%= form.income %><br /> > <%= form.id %> <br /> > <%= form.income %><br /> > <% end %> > > It gives me the wrong id i want to display..I want to display id in > payments. > > -- > 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.
Ad Ber
2011-Apr-11 08:54 UTC
Re: Display specific field that has duplicate name to other table
Frederick Cheung wrote in post #992073:> On Apr 11, 8:11am, Ad Ber <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> In my ruby on rails action or method Controller: >> def print_two_table >> @p = Pay.find_by_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM >> pays as P LEFT OUTER JOIN suggests AS s ON s.b_id = p.b_id WHERE >> p.created_at BETWEEN ''2008-03-04'' AND ''2008-07-06'' ") >> end > If you''re going to do it like this (rather than using associations) > you''re going to need to alias the column names (ie select s.id as > suggest_id) > > FredUhm..I have already made the necessary relationships..do you have any idea how i can be able to display the proper id want to display? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-11 09:14 UTC
Re: Display specific field that has duplicate name to other table
On Apr 11, 9:54 am, Ad Ber <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #992073: > > > On Apr 11, 8:11am, Ad Ber <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> In my ruby on rails action or method Controller: > >> def print_two_table > >> @p = Pay.find_by_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM > >> pays as P LEFT OUTER JOIN suggests AS s ON s.b_id = p.b_id WHERE > >> p.created_at BETWEEN ''2008-03-04'' AND ''2008-07-06'' ") > >> end > > If you''re going to do it like this (rather than using associations) > > you''re going to need to alias the column names (ie select s.id as > > suggest_id) > > > Fred > > Uhm..I have already made the necessary relationships..do you have any > idea how i can be able to display the proper id want to display?Well you''re not using them. if you were You''d do something like Pay.where( ...).includes(:suggests) and then iterate over your pay objects and their associated suggest objects. If you don''t want to do that then like I said you need to alias the ambiguous column names so that they aren''t ambiguous anymore. Fred> > -- > 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.