Ruby on rails Rails与使用STI的表有多个:到:关联

Ruby on rails Rails与使用STI的表有多个:到:关联,ruby-on-rails,activerecord,associations,has-many,single-table-inheritance,Ruby On Rails,Activerecord,Associations,Has Many,Single Table Inheritance,我不知道该怎么做;我已经研究了其他的SO问题,包括和,但没有结果 以下是我的模型: class User < ActiveRecord::Base has_many :connections has_many :data_sources, through: :connections end class Connection < ActiveRecord::Base belongs_to :user belongs_to :data_source end class

我不知道该怎么做;我已经研究了其他的SO问题,包括和,但没有结果

以下是我的模型:

class User < ActiveRecord::Base
  has_many :connections
  has_many :data_sources, through: :connections
end

class Connection < ActiveRecord::Base
  belongs_to :user
  belongs_to :data_source
end

class DataSource < ActiveRecord::Base
  has_many :connections
  has_many :users, through: :connections
end

class AdNetwork < DataSource
end

class Marketplace < DataSource
end
我还尝试在
连接中添加一列
data\u source\u type
,并添加以下行

类连接 属于:数据源,多态:真
结束

类数据源 有多个:连接,如::数据源 结束

类用户 有很多:市场,通过::连接,来源::数据来源,来源类型:“市场” 结束

。。。但是现在,
#marketplaces
返回一个空关系。(看起来,
data\u source\u type
总是被设置为
DataSource
,即使我添加了一个市场或AdNetwork。)

我做错了什么

class User
  has_many :marketplaces, through: :data_sources # Throws an error
  has_many :marketplaces, through: :connections # Throws an error
  has_many :marketplaces, through: :connections, source: :data_source # Returns ALL data sources, not just Marketplaces
  has_many :marketplaces, { through: :connections, source: :data_source }, -> { where(type: "Marketplace" } # Again this returns ALL data sources. Wtf???
end