こんにちは私は周りを見回して答えを見つけることができません。条件付き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レールは、事前にありがとうございます。
あなたはここからきているもの:ああ上のホールド、あなたは最初の製品
のでに参加する必要がありますか? 'Outfit.find(1).approved_products.where( 'enabled =?'、true)' – Sravan
有効になっているモデルが – Fallenhero