Hello Everyone, I am having problems in implementing joins in ror. as I used [code]@users=User.joins(''LEFT OUTER JOIN donations ON donations.user_id = users.id'')[/code] I also tried [code]@users=User.find_by_sql "select u.*, d.amount from users u left join donations d on u.id=d.user_id"[/code] but since model name is user it only displays all records of users table not a single record of donations table. I want to display records of both tables users and donations with the help of foreign key user_id in donations. Thanks in advance -- 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.
Just create an association in user has_many donations this way you don''t have to write queries. To get a list of donations made by a user just make a call @user.donations On Wed, Mar 2, 2011 at 1:26 PM, Kanika S. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello Everyone, > I am having problems in implementing joins in ror. as I used > > [code]@users=User.joins(''LEFT OUTER JOIN donations ON donations.user_id > = users.id'')[/code] > > I also tried > > [code]@users=User.find_by_sql "select u.*, d.amount from users u > left join donations d on u.id=d.user_id"[/code] > > but since model name is user it only displays all records of users table > not a single record of donations table. > I want to display records of both tables users and donations with the > help of foreign key user_id in donations. > > Thanks in advance > > -- > 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. > >-- Rohit Pal -- 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.
On 2 March 2011 08:12, rohit pal <rohitpal123-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just create an association in user > has_many donations > this way you don''t have to write queries. > To get a list of donations made by a user just make a call -vIhTp3xOtoOzZXS1Dc/lvw@public.gmane.orgionsIn addition see the Rails Guide on ActiveRecord Relationships for more information. Colin> > > On Wed, Mar 2, 2011 at 1:26 PM, Kanika S. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Hello Everyone, >> I am having problems in implementing joins in ror. as I used >> >> [code]@users=User.joins(''LEFT OUTER JOIN donations ON donations.user_id >> = users.id'')[/code] >> >> I also tried >> >> [code]@users=User.find_by_sql "select u.*, d.amount from users u >> left join donations d on u.id=d.user_id"[/code] >> >> but since model name is user it only displays all records of users table >> not a single record of donations table. >> I want to display records of both tables users and donations with the >> help of foreign key user_id in donations. >> >> Thanks in advance >> >> -- >> 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. >> > > > > -- > Rohit Pal > > -- > 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. >-- 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.