Hi. I have a project based on Rails 3.0.3 and MySQL. There are 2 tables (not all columns shown): 1. users (id, name, group_id) class User < ActiveRecord::Base belongs_to :groups end 2. groups (id, name) class Group < ActiveRecord::Base has_many :users end Console output: irb(main):001:0> User.first => #<User id: 1, name: "John Smith", group_id: 3> How can i do that besides the "group_id: 3" result the query will return the Group name too. For example: irb(main):001:0> User.first => #<User id: 1, name: "John Smith", group_id: 3, group: "Administrator"> I can do this by using the JOIN statement in mysql, but i need to use raw sql queries. May be there is a way to do it by using ActiveRecord style ? -- 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 17 February 2011 21:50, jsmax <popov.max-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> irb(main):001:0> User.first > => #<User id: 1, name: "John Smith", group_id: 3> > > How can i do that besides the "group_id: 3" result the query will > return the Group name too. For example:Have a look at the documentation: http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html "Eager loading" is probably your friend, but you can also do what you want by changing the :select option of any AR query. -- 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 17 February 2011 21:50, jsmax <popov.max-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi. I have a project based on Rails 3.0.3 and MySQL. > > There are 2 tables (not all columns shown): > > 1. users (id, name, group_id) > > class User < ActiveRecord::Base > belongs_to :groupsThat should be :group (singular), user belongs to one group so singular.> end > > 2. groups (id, name) > > class Group < ActiveRecord::Base > has_many :users > end > > Console output: > > irb(main):001:0> User.first > => #<User id: 1, name: "John Smith", group_id: 3> > > How can i do that besides the "group_id: 3" result the query will > return the Group name too. For example:You can just say User.first.group.name or user = User.first group_name = user.group.name Rails will fetch the group from the database when you reference it. Alternatively you may use the :include option on find to force it to fetch the user and group in one query if you are running into performance problems. Colin -- 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.