问题
I'm trying to order Tags by order of the descending frequency of their association with Users of a specific Group. (ActiveRecord & Rails 3.2 - I also have Squeel installed if that helps!)
Users have Tags, Groups have Users. Groups thus have Tags through Users.
class Group < ActiveRecord::Base
has_many :users_groups # join table
has_many :users, through: :users_groups
has_many :tags, through: :users
class User < ActiveRecord::Base
has_many :users_groups # join table
has_many :groups, through: :users_groups
has_many :taggings # join table
has_many :tags, through: taggings
class Tag < ActiveRecord::Base
has_many :taggings # join table
has_many :users, through: taggings
These answers from Steve [ https://stackoverflow.com/a/11624005/59195 ] and Amol [ https://stackoverflow.com/a/10958311/59195 ] come closest to what I'm trying to do — though I want to order the tags only by their frequency of use/association by users who are members of the group, not all users.
Any ideas? Thank you for your help!
来源:https://stackoverflow.com/questions/19537378/ordering-nested-has-many-through-by-count-of-associations