Question

I have written quite a few AREL statements, but I'm tying myself in knots over this one. Here is my situation:

class Product < AR::Base
  has_many :parents, :class_name => "ProductLink", :foreign_key => :to_product_id
  has_many :children, :class_name => "ProductLink", :foreign_key => :from_product_id

  # has an attribute called "identifier"

end

class ProductLink < AR::Base
  belongs_to :parent, :class_name => "Product", :foreign_key => :from_product_id
  belongs_to :child, :class_name => "Product", :foreign_key => :to_product_id
end

I want to retrieve all of the Products that have a child product with an identifier that matches some value.

I have twisted myself into a pretzel with this, seems easy, but I have been looking at it for too long now. I appreciate any help!

Was it helpful?

Solution

Got it!

brand.products.joins(:children => :child).where(:children => { :child => { :searchable_identifier.matches => "2136" } } )

That works great. See the hashed joins? That's what was throwing me off.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top