2016-11-28 14 views
-1

こんにちは私は周りを見回して答えを見つけることができません。条件付きHas_many

私は衣装に使用可能なすべての承認済み製品を取得しようとしています。イムは、これは、ここで条件を使用して行うことができる思考:

outfit.rb 

has_many :products, through: :outfit_products 
has_many :approved_outfit_products, -> { where(approved: true) }, 
     class_name: 'TaggedProduct', dependent: :destroy 
has_many :approved_products, through: :approved_outfit_products, source: :product, dependent: :destroy 

Outfit.find(1).approved_productsは私にされている製品のリストを返します。ここ

has_many :approved_outfit_products, -> { where(approved: true) }, 
     class_name: 'TaggedProduct', dependent: :destroy 

いくつかの余分な情報があります承認されたこの商品リストのうち、を有効にしたものが必要です。ここでは、より良い何が起こっているか

を理解するために

$ rails c 
PLoading development environment (Rails 4.2.0) 
ro2.1.2 :001 > Product.new 
=> #<Product id: nil, title: nil, description: nil, created_at: nil, updated_at: nil, user_id: nil, category_id: nil, size_description: nil, shipping_description: nil, price_in_cents: nil, enabled: true> 
2.1.2 :002 > TaggedProduct.new 
=> #<TaggedProduct id: nil, product_id: nil, outfit_id: nil, approved: false, created_at: nil, updated_at: nil, receiver_id: nil, requester_id: nil> 

Cレールは、事前にありがとうございます。

+0

あなたはここからきているもの:ああ上のホールド、あなたは最初の製品

ので

に参加する必要がありますか? 'Outfit.find(1).approved_products.where( 'enabled =?'、true)' – Sravan

+0

有効になっているモデルが – Fallenhero

答えて

2

あなたはチェーンwhereがちょうど.where(enabled: true)

編集]を追加することができます。

joins(:product).where(product: {enabled: true}) 
+0

であるため、私の答えを見てください。承認されたものはTaggedProoductsテーブルにあります。有効になっているのはProductテーブルです。 – joeyk16

+0

それはそうです。ありがとうございました。 – joeyk16