【发布时间】:2011-02-08 13:57:09
【问题描述】:
假设我在用户和组之间有一个多对多的关系。用户可以是组的成员,或者他的申请仍处于待处理状态。
class User < ActiveRecord::Base
has_many :applications
has_many :groups, :through => :applications
end
class Group < ActiveRecord::Base
has_many :applications
has_many :users, :through => :applications
end
class Application < ActiveRecord::Base
belongs_to :user
belongs_to :group
attr_accessible :pending # boolean : is the application still pending or not
end
我想为我的 Group 类添加一个范围,以选择拥有超过 10 个非待定用户的组。
我可以得到这样的成员
Group.joins(:applications).where('applications.pending = ?', false)
但我没有找到足够的资源来创建计算此查询结果数量的范围,并返回此数字大于 10 的组
如果您对此主题有解决方案或资源,这将对我有很大帮助
【问题讨论】:
标签: sql ruby-on-rails activerecord count named-scope