Ruby on rails ActiveRecord有很多:通过多个源的关联

Ruby on rails ActiveRecord有很多:通过多个源的关联,ruby-on-rails,ruby,activerecord,associations,has-many-through,Ruby On Rails,Ruby,Activerecord,Associations,Has Many Through,我有一个自引用has\u many:通过另一个has\u和属于另一个模型的多个模型。基本上是这样的: class Foo << ActiveRecord::Base has_and_belongs_to_many :bars has_many :foo_links has_many :foo_parents, :through => :foo_links, :foreign_key => :foo_parent_id, :class_name => "F

我有一个自引用has\u many:通过另一个has\u和属于另一个模型的多个模型。基本上是这样的:

class Foo << ActiveRecord::Base
  has_and_belongs_to_many :bars
  has_many :foo_links
  has_many :foo_parents, :through => :foo_links, :foreign_key => :foo_parent_id, :class_name => "Foo"
  has_many :foo_children, :through => :foo_links, :foreign_key => :foo_child_id, :class_name => "Foo"
end
has_many :inherited_bars, :through => :foo_parents, :source => [:bars, :inherited_bars]

我从未见过这样的例子,但我想知道是否有可能有一个关联,它是从一个直通关联合并而来的关联。

我认为有很多关联总是绑定在某个地方有一个id来指示关系,并允许您修改它。您可以将新元素添加到has_many数组中,并将结果持久化回数据库。如果您可以将两个源合并在一起,那么您将失去通过该方法链接行的能力

一种可能的方法是这种只读方式:

has_many :a
has_many :b

def sum
  a + b
end