It sounds like the issue is with how to make your find_by_product
scope work when the passed in product is nil? I think your curly braces are a little mixed up. Either way, here's how I'd write it in case this helps:
ProductConnection
scope :for_product, ->(product) {
if product_id = product.try(:id)
where(arel_table[:product_a_id].eq(product_id).
or(arel_table[:product_b_id].eq(produt_id))
end
}
Then, once that works, you can just call destroy_all on the scope to destroy all the records.
ProductConnection.for_product(my_product).destroy_all
There is also delete_all, which you can use in the same way, if you really don't want the ActiveRecord callbacks included with destroy
.