Forum: Ruby on Rails using find with multiple objects in active record

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0ebe4d5a7e91575265ac1a96ed49aa57?d=identicon&s=25 Sutee Dee (yoshibot)
on 2006-04-01 04:08

I am trying to learn to use RoR by making a simple social networking
example. Given a user id, I am trying to make a query that finds all of
the group that user belongs to but also rank the groups by one of the
groups' parameter like group.size. Pretend group.size is a number from
1-10 and is precalculated. Can I do this is one query? Here are the
relationships I set up:

class User < ActiveRecord::Base
	has_and_belongs_to_many :groups

class Groupship < ActiveRecord::Base
	set_table_name "users_groups"
	belongs_to :recipes
	belongs_to :tags

class Group < ActiveRecord::Base
	has_and_belongs_to_many :users

59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-01 09:02
(Received via mailing list)
This should do it.

groups = user.groups.find(:all,:order => 'size')

-- Tom Mornini
This topic is locked and can not be replied to.