0

我正在使用 Rails 2.3.11。

class ModelA < ActiveRecord::Base
  belongs_to :modelb
  has_one :modelc, :through => :modelb
end

class ModelB < ActiveRecord::Base
  belongs_to :modelc
end

class ModelC < ActiveRecord::Base
  has_many :modelb
end

a = ModelA.find(x)
a.modelc # is nil (why??)
a.modelb.modelc # works fine

有不同的方法吗?

4

1 回答 1

0

我发现了问题,

inst_modelb = Factory(:modelb, :modelc => Factory(:modelc))
a=Factory(:modela, :modelb => inst_modelb)
a.modelc # was nil

#this worked
a.reload
a.modelc

它似乎是一个缓存问题。虽然不能完全推理

于 2012-05-17T14:08:03.010 回答