There is a join table with three columns: id
, product_a_id
, product_b_id
class ProductConnection < ActiveRecord::Base
belongs_to :product_a, class_name: :Product
belongs_to :product_b, class_name: :Product
end
I would like to filter the table by a specific product regardless in which column the product id is contained. How can I write a named scope which respects that a product
can be nil
? The following draft is inspired by a post by Zack Holman though it does not work:
scope :find_by_product, \
lambda {|p| p.nil? ? { \
where(["product_a_id = ? OR product_b_id = ?", p.id, p.id]) : {} \
}
Then I would like to know how I can delete all products returned in the ActiveRecord::Relation
?