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

时间:2013-07-09 06:19:23

标签: ruby-on-rails activerecord associations has-many-through has-many

所以这是一个示例类

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

2 个答案:

答案 0 :(得分:14)

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

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

话虽如此,假设您可以像投资者那样使用继承:

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

答案 1 :(得分:2)

我在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的阅读非常有帮助。

希望它有所帮助!