Rails has_many through aliasing with source and source_type for multiple types

Michael K Madison picture Michael K Madison · Jul 9, 2013 · Viewed 11.4k times · Source

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

Answer

polmiro picture polmiro · Jul 24, 2013

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