簡體   English   中英

如何在對象和關聯對象中同時處理條件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM