Ruby on rails Rails通过对多种类型的source和source类型进行别名来实现多种功能

Ruby on rails Rails通过对多种类型的source和source类型进行别名来实现多种功能,ruby-on-rails,activerecord,associations,has-many-through,has-many,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

下面是一个示例类

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
class公司
@company.angels和@company.vc_公司按预期工作。但我怎么会有@company.investors同时包含这两种来源类型呢?这对Investments表的investor列上的所有多态性都有效吗?或者是一种使用作用域合并所有源类型的方法

投资模型如下所示:

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 Investor < ActiveRecord::Base
  has_many :investments
end

class VcFirm < Investor
end

class Angel < Investor
end
类投资
天使通过人物模型进行关联

class Person < ActiveRecord::Base
    has_many :investments, as: :investor
end
class-Person
相关金融组织示范协会:

class FinancialOrganization < ActiveRecord::Base
    has_many :investments, as: :investor
    has_many :companies, through: :investments
end
class Financial Organization
之前的解决方案是错误的,我误解了其中一个关系

Rails无法为您提供跨越多态关系的has\u many方法。原因是实例分布在不同的表中(因为它们可能属于不同的模型,可能在同一个表上,也可能不在同一个表上)。因此,如果您跨越了一个属于多态关系,则必须提供源类型

话虽如此,假设你可以像这样在投资者中使用遗产:

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 Investor < ActiveRecord::Base
  has_many :investments
end

class VcFirm < Investor
end

class Angel < Investor
end
class Investor
您将能够从投资中删除多态选项:

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
class公司{:投资者=>{:类型=>'VcFirm'}}
有很多:天使,通过::投资,来源::投资者,条件:=>{:investors=>{:type=>'Angel'}
结束

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

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
class公司:投资,:来源=>:投资者,:来源类型=>“风险投资公司”
有很多:天使,:通过=>:investments,:source=>:investor,:source\u type=>'Angel'
def投资者
joins(:investments).where(:investments=>{:company_id=>id})
结束
结束
在阅读
:source
:source\u type
时看起来非常有用


希望有帮助

不起作用。以下是错误:ActiveRecord::HasmanyThroughAssociation多态资源错误:在多态对象“Investor#Investor”上不能有一个has_many:through association“Company#investors”,而没有“source#type”。尝试将“来源类型:“投资者”添加到“通过”定义中。嗯。。。然后我不得不将人员和金融机构放在同一张桌子上,这让我感觉不对劲。我告诉过你你有其他选择。你不能使用你想要的关系,因为它不起作用。如果您只想检索两者的列表,请在公司“def investors;vc_firms+angels;end”上创建一个方法。