Min Ming Lo
Min Ming Lo

Reputation: 2508

Rails Nested Joins

Manufacturer

has_many :product

Product

has_many :part

PartsRequest

belongs_to :part
belongs_to :manufacturer 

I am trying to do something in PartsRequest e.g. Manufacturer A is trying to request for Parts from Manufacturer B

scope :incoming_requests, lambda { |manufacturer_id|
    joins(:part).joins(product).where("product.manufacturer_id = ?", manufacturer_id)
}

How do I join to parts, then from parts join with products?

Upvotes: 1

Views: 2881

Answers (1)

mnelson
mnelson

Reputation: 3012

scope :incoming_requests, lambda{|mid| joins(:part => :product).where(:product => {:manufacturer_id => mid}) }

Upvotes: 4

Related Questions