ruby on rails - Hard time creating a class scope for HABTM relation -
this simplified situation:
class producer has_and_belongs_to_many :rules end class rule has_and_belongs_to_many :producers end but rule happens follow sti structure. 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 fine. want create scope returns producers related productrules
something equivalent this:
producer.all.select{|x| x.product_rules.any? } can point out quick solution?
note: obvioulsy don't want load producers , select them afterward, want directly load right ones
update
im using rails version 2.3.15
every association between producer , 1 of rule's subclasses going create separate record on join table. can use fact , select producers have record on join table pointing them (taking care select correct type):
in rails 2.x:
class producer def self.with_some_product_rule scoped(conditions: <<-sql) producers.id in ( select producer_id producers_rules inner join rules on rules.id = producers_rules.rule_id rules.type = 'productrule' ) sql end end
Comments
Post a Comment