有沒有一種方法可以獲得客戶訂購特定產品的結果?我有我的模型設置是這樣的:在Rails中找到客戶訂購的產品
class Customer < ActiveRecord::Base
has_many :order_items
has_many :products, :through => :order_items
end
class OrderItem < ActiveRecord::Base
belongs_to :product
belongs_to :customer
end
class Product < ActiveRecord::Base
has_many :order_items
has_many :customers, :through => :order_items
belongs_to :category
end
理想情況下,我想能夠寫在紅寶石的東西,會回到什麼回報客戶的陣列已經訂購了某種產品。基本上,我想要寫的東西,如:
Customer.product.where(:name => "Widget").first
而且有工作,並在已下令該產品數據庫返回的第一個客戶。基本上搜索產品表的名稱字段,返回一個product_id,然後使用該product_id查看order_items表中的哪些customer_id與它關聯,並使用這些customer_id返回與這些customer_id相關聯的customers表中的字段
你想要一個客戶,或所有已訂購產品的客戶嗎? –