On 17 February 2011 21:50, jsmax <popov....@gmail.com> 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 :groups
That 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@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.