![](/img/trans.png)
[英]How do I elegantly check for presence of both the object and associated objects?
[英]how to do a where with a condition both in the object and in an associated object
我有兩個類ProductList和List。 ProductList將產品關聯到列表中。 列表可以具有不同的類型(list_type_id)。 一個ProductList has_many產品和has_many列表。
我想取回屬於特定list_type_id的特定product_id的計數。
我在想這樣的事情:
ProductList.where('product_id=123').where('list.list_type_id=7').count
但這似乎不起作用。 有任何想法嗎?
謝謝
ProductList.joins(:lists)
.where("product_lists.product_id = ? AND lists.list_type_id = ?", 123, 7)
我不確定product_lists
,也許應該像productlists
一樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.