0

Well, I know how to handle associations from one model (call it ModelA) through the second one (ModelB) to the third one (ModelC). But what if the third one(ModelC) is associated to the fith one(ModelE) trough the 4th (ModelD).

#ModelA 
has_many :model_bs
has_many :model_cs, :trough => :model_bs

#ModelC
has_many :model_ds
has_many :model_es, :trough => model_ds

The question is: how can I get the collection of ModelE records, associated with the specific ModelA record trough that chain?

4

1 回答 1

2

调用model_cs的实例ModelA将返回 的数组model_cs。如果你想访问所有model_es你需要遍历数组model_cs并将这些结果附加到数组或散列中。

array1 = @model_a.model_cs.all
array2 = []
array1.each do |x|
  array2 << x.model_es
end

array2现在包含所有model_es其最终父级是 original model_a

于 2012-03-19T13:53:58.560 回答