Every association between Producer
and one of Rule
's subclasses is going to create a separate record on the join table. You can use that fact and select all the Producer
s that have any record on the join table pointing to them (taking care to select the correct type
):
In Rails 2.x:
class Producer
def self.with_some_product_rule
scoped(conditions: <<-SQL)
producers.id IN (
SELECT producer_id FROM producers_rules
INNER JOIN rules ON rules.id = producers_rules.rule_id
WHERE rules.type = 'ProductRule'
)
SQL
end
end