1
假设我在用户和组之间有多对多的关系。用户可以是一个组的成员,或者他的应用程序仍然可以处于等待状态。Rails - 命名范围问题(取决于many_to_many关系中的子项数)
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
如果你有一个解决方案,或ressources关于这个主题,这会对我们帮助很大
工程很棒。谢谢! – MrRuru 2011-02-09 02:16:03