ActiveRecord, has_many :through, Polymorphic Associations with STI

前端 未结 1 1044
忘了有多久
忘了有多久 2020-12-16 22:25

In ActiveRecord, has_many :through, and Polymorphic Associations, the OP\'s example requests ignoring the possible superclass of Alien and Person <

相关标签:
1条回答
  • 2020-12-16 22:41

    Have you tried the simplest thing - adding :conditions to the has_many :throughs?

    In other words, something like this (in widget.rb):

    has_many :people, :through => :widget_groupings, :conditions => { :type => 'Person' }, :source => :grouper, :source_type => 'SentientBeing'
    has_many :aliens, :through => :widget_groupings, :conditions => { :type => 'Alien' }, :source => :grouper, :source_type => 'SentientBeing'
    

    JamesDS is correct that a join is needed - but it's not written out here, since the has_many :through association is already doing it.

    0 讨论(0)
提交回复
热议问题