范围过滤器记录取决于关联计数

时间:2014-11-03 14:47:57

标签: ruby-on-rails mongodb scope has-many

我使用了MongoDB,我的模型retailer有很多stores,我想要返回至少有一个商店的零售商。

我发现了这个问题:How can I find records by “count” of association using rails and mongoid?

但接受的答案是:

scope :with_at_least_n_stores, -> { where('stores_count >= 1') }

取决于零售商模式中的stores_count字段,但我没有此字段。

找到零售商的任何解决方案,至少有一家商店使用范围,而不添加stores_count字段?

1 个答案:

答案 0 :(得分:0)

也许这个?

scope :with_at_least_n_stores, -> { stores.size > 0 }