我在为活动记录关联进行布线时经常遇到问题,我期待着最终了解是什么导致了它(而不仅仅是解决它)。为什么我需要重新加载这个儿童关联?
当通过parent.children < <将孩子与父母关联时,对孩子的引用正确地更新自己。如果关系建立反向(如果通过child.parent =完成),则这不起作用。
为什么会发生这种情况?有没有办法使双方的关系?我试过inverse_of
以前没有成功。我希望是因为我对这里发生的事情不够了解。
例子:
鉴于这些模型:
class Task < ActiveRecord::Base
belongs_to :batch
attr_accessor :state
end
class Batch < ActiveRecord::Base
has_many :tasks
def change_tasks
tasks.each { |x| x.state = "started" }
end
end
为什么第一规格失败,但第二遍?我可以进行第一次传球吗?出于某种原因,我需要重新加载第一个规格,但不是在第二个规格。
describe "avoiding reload" do
context "when association established via child.parent=" do
it "updates child references" do
b = Batch.create
t = Task.create(batch: b)
b.change_tasks
b.tasks[0].state.should == "started" # passes
t.state.should == "started" # fails!?
t.reload.state.should == "started" # passes, note the reload
end
end
context "when association established via parent.children<<" do
it "updates child references" do
b = Batch.create
t = Task.create
b.tasks << t
b.change_tasks
b.tasks[0].state.should == "started" # passes
t.state.should == "started" # passes
end
end
end
感谢您的帮助。