Ordering nested has_many :through by count of associations

这一生的挚爱 提交于 2019-12-04 06:21:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!