Rails has_many通过使用source和source_type别名来处理多种类型

所以这是一个示例类

class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end 

@ company.angels和@ company.vc_firms按预期工作。 但是,我如何拥有由两种源类型组成的@ company.investors? 这适用于投资表中投资者专栏的所有多态? 或者也许是使用范围合并所有source_type的方法?

投资模式如下:

 class Investment < ActiveRecord::Base belongs_to :investor, polymorphic: true belongs_to :company validates :funding_series, presence: true #, uniqueness: {scope: :company} validates :funded_year, presence: true, numericality: true end 

天使通过人物模型联系在一起

 class Person < ActiveRecord::Base has_many :investments, as: :investor end 

相关金融机构模型协会:

 class FinancialOrganization < ActiveRecord::Base has_many :investments, as: :investor has_many :companies, through: :investments end 

以前的解决方案是错误的,我误解了其中一个关系。

Rails无法为您提供跨越多态关系的has_many方法。 原因是实例通过不同的表扩展(因为它们可以属于可能或不在同一个表上的不同模型)。 因此,如果您跨越belongs_to多态关系,则必须提供source_type。

话虽如此,假设您可以在投资者中使用inheritance,如下所示:

 class Investor < ActiveRecord::Base has_many :investments end class VcFirm < Investor end class Angel < Investor end 

您可以从投资中删除多态选项:

 class Investment < ActiveRecord::Base belongs_to :investor belongs_to :company ......... end 

并且您将能够跨越关系并将其范围与条件:

 class Company < ActiveRecord::Base has_many :investments has_many :investors, through :investments has_many :vc_firms, through: :investments, source: :investor, conditions: => { :investors => { :type => 'VcFirm'} } has_many :angels, through: :investments, source: :investor, conditions: => { :investors => { :type => 'Angel'} } end 

我在Company类中添加了一种方法,通过加入投资表来获取Company所有投资者:

 class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, :through => :investments, :source => :investor, :source_type => 'VcFirm' has_many :angels, :through => :investments, :source => :investor, :source_type => 'Angel' def investors Investor.joins(:investments).where(:investments => {:company_id => id}) end end 

http://www.brentmc79.com/posts/polymorphic-many-to-many-associations-in-rails看起来非常有助于阅读:source :source_type

希望能帮助到你!