因此,这里是一个示例类导轨的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
天使通过Person模型相关
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
不起作用。以下是错误:ActiveRecord :: HasManyThroughAssociationPolymorphicSourceError:通过关联'公司#投资者'关联多态对象'投资者#投资者'而没有'source_type',不能拥有has_many:。尝试添加'source_type:'投资者''到'has_many:通过'定义。 –
@MichaelKMadison我更正了答案 – polmiro
嗯...然后我不得不将人和金融机构存储在同一张表中,这感觉不对。 –