2014-11-03 40 views
1

我使用了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

回答

0

也許這個?

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

不,我之前試了一下,得到這個錯誤'NameError:對於零售商未定義的局部變量或方法「商店」:Class' – 2014-11-04 02:03:58

+0

好了,看到這裏由帕特里克的答案:http://stackoverflow.com /問題/ 8223518 /如何-可以-I-發現,記錄按計數的關聯,用護欄和-mongoid – 2014-11-04 06:04:59

相關問題