This is my simplified situation:
class Producer
has_and_belongs_to_many :rules
end
class Rule
has_and_belongs_to_many :producers
end
But Rule
happens to follow a STI structure. So I have this:
class Producer
has_and_belongs_to_many :rules
has_and_belongs_to_many :product_rules, join_table: :producers_rules,
association_foreign_key: :rule_id
has_and_belongs_to_many :fee_rules, join_table: :producers_rules
association_foreign_key: :rule_id
end
class Rule
has_and_belongs_to_many :producers
end
class ProductRule < Rule
end
class FeeRule < Rule
end
No big deal, works out just fine. So now I want to create a scope that returns only Producers
that are related to ProductRules
Something equivalent to this:
Producer.all.select{|x| x.product_rules.any? }
Can anyone point out a quick solution?
NOTE: I obvioulsy don't want to load all producers and select them afterward, I want to directly load only the right ones
UPDATE
Im using Rails version 2.3.15