2013-07-24 55 views
0

我需要一種方法根據其相關模型之一的條件查找模型中的所有記錄。
事情是這樣的:根據相關模型的條件查找模型中的記錄

Product.where(:product_number => [1,3,5], customer.city => "New New York") 

這將返回所有的產品,其客戶的城市是新的紐約,有兩種1,3或5

我的產品表中的產品數有CUSTOMER_ID列,所以我可以找到所有紐約紐約市的顧客,拿起他們的身份證,然後使用:customer_id => customerIdArray,但有沒有更簡單的方法,就像我的例子一樣?也許使用類似customer.citycustomer[:city]

回答

1

嘗試以下操作:

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') 
+0

那麼它看起來就像是沒有的伎倆。非常感謝,夥伴。願你的劍保持鋒利,你的馬駒會很快。 – notblakeshelton

相關問題