Hi, 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 end class Groupship < ActiveRecord::Base set_table_name "users_groups" belongs_to :recipes belongs_to :tags end class Group < ActiveRecord::Base has_and_belongs_to_many :users end Thanks! -- Posted via http://www.ruby-forum.com/.
Tom Mornini
2006-Apr-01 07:01 UTC
[Rails] using find with multiple objects in active record
This should do it. groups = user.groups.find(:all,:order => ''size'') -- -- Tom Mornini On Mar 31, 2006, at 6:08 PM, Sutee Dee wrote:> 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 > end > > class Groupship < ActiveRecord::Base > set_table_name "users_groups" > belongs_to :recipes > belongs_to :tags > end > > class Group < ActiveRecord::Base > has_and_belongs_to_many :users > end > > Thanks! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails