belongs_to with has_many through

时间:2012-05-17 03:48:00

标签: ruby-on-rails activerecord

我使用的是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

有不同的方法吗?

1 个答案:

答案 0 :(得分:0)

我找到了问题,

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

#this worked
a.reload
a.modelc

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