[英]Rails query one model based on the lack of an associated model with conditions
[英]find records in model based on conditions of associated model
我需要一種方法來根據其關聯模型之一的條件查找模型中的所有記錄。
像這樣:
Product.where(:product_number => [1,3,5], customer.city => "New New York")
它將返回客戶所在城市為紐約且產品編號為1、3或5的所有產品。
我的產品表中有一個customer_id列,因此我很可能可以找到所有城市為紐約的客戶,獲取其ID,然后使用:customer_id => customerIdArray
,但是有沒有更簡單的方法,例如在我的示例中? 也許使用諸如customer.city
或customer[:city]
?
請嘗試以下操作:
Product.joins(:customer).where('products.product_number in (:product_list) and customers.city = :customer_city', product_list: [1, 3, 5], customer_city: 'New New York')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.