Rails has_many through aliasing with source and source_type for multiple types

走远了吗. 提交于 2019-12-03 16:59:23

问题


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

回答1:


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



回答2:


I added a method to the Company class that fetches all investors for the company by joining with the investments table:

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 looked pretty helpful for reading up on :source vs. :source_type.

Hope it helps!



来源:https://stackoverflow.com/questions/17541277/rails-has-many-through-aliasing-with-source-and-source-type-for-multiple-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!