これは私の単純化された状況です:
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を使用しています