通过与源和SOURCE_TYPE多种类型的别名导轨的has_many [英] Rails has_many through aliasing with source and source_type for multiple types

查看:166
本文介绍了通过与源和SOURCE_TYPE多种类型的别名导轨的has_many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里是一个示例类

 类公司< ActiveRecord的::基地
    的has_many:投资
    的has_many:vc_firms,通过:投资,来源:投资者,SOURCE_TYPE:VentureFirm
    的has_many:天使,通过:投资,来源:投资者,SOURCE_TYPE:'人'
结束

@ company.angels和@ company.vc_firms按预期工作。但是,我怎么会有@这是由两种源类型的company.investors?这将工作的投资表的投资者列的所有polymorphics?或者使用范围,合并所有SOURCE_TYPE的一种方式?

投资模式是这样的:

 类投资LT; ActiveRecord的::基地
  belongs_to的:投资人,多态:真
  belongs_to的:公司  验证:funding_series,presence:真#,独特性:{范围:公司}
  验证:funded_year,presence:真的,numericality:真
结束

天使是通过人模型相关联

 类Person< ActiveRecord的::基地
    的has_many:投资,:投资者
结束

相关金融组织模型关联:

 类FinancialOrganization< ActiveRecord的::基地
    的has_many:投资,:投资者
    的has_many:公司,通过:投资
结束


解决方案

previous解错了,我误解了关系之一。

Rails可以不向您提供的has_many方法穿越一个多态的关系。原因是通过不同的表的情况下,有s $ P $垫出来(因为他们可以属于不同的车型可能是或不是在同一个表)。所以,如果你越过belongs_to的多态关系,你必须提供SOURCE_TYPE。

话虽如此,假如你能在这样的投资者使用继承:

 类投资者< ActiveRecord的::基地
  的has_many:投资
结束类VcFirm<投资者
结束类天使<投资者
结束

在你将能够去除投资多态性选项:

 类投资LT; ActiveRecord的::基地
  belongs_to的:投资者
  belongs_to的:公司  .........
结束

而你将能够穿越关系和范围将其与条件:

 类公司< ActiveRecord的::基地
    的has_many:投资
    的has_many:投资者通过:投资
    的has_many:vc_firms,通过:投资,来源:投资者,条件:=> {:投资= GT; {:类型=> VcFirm'}}
    的has_many:天使,通过:投资,来源:投资者,条件:=> {:投资= GT; {:类型=> '天使'}}
结束

So here is a sample class

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 and @company.vc_firms works as expected. But how would I have @company.investors that are comprised of both source types? That would work for all polymorphics on the investor column of the Investments table? or perhaps a way of using a scope to merge all source_type?

Investment model looks like this:

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

Angels are associated through the Person model

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

Relevant financial organization model associations:

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

解决方案

Previous solution was wrong, I misunderstood one of the relations.

Rails cannot provide you with a has_many method crossing a polymorphic relation. The reason is that the instances are spread out through different tables (because they can belong to different models which might or not be on the same table). So, you must provide the source_type if you cross a belongs_to polymorphic relation.

Having said that, supposing you could use inheritance in the Investor like this:

class Investor < ActiveRecord::Base
  has_many :investments
end

class VcFirm < Investor
end

class Angel < Investor
end

The you would be able to remove the polymorphic option from investments:

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

  .........
end

And you would be able to cross the relation and scope it with conditions:

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

这篇关于通过与源和SOURCE_TYPE多种类型的别名导轨的has_many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆