这是我的简化情况:
class Producer
has_and_belongs_to_many :rules
end
class Rule
has_and_belongs_to_many :producers
end
但Rule
恰好遵循 STI 结构。所以我有这个:
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
没什么大不了的,效果很好。所以现在我想创建一个只返回Producers
与ProductRules
与此等价的东西:
Producer.all.select{|x| x.product_rules.any? }
谁能指出一个快速的解决方案?
注意:我显然不想加载所有生产者并在之后选择它们,我只想直接加载正确的
更新
我正在使用 Rails 版本 2.3.15