我发现(在脚本/控制台中闲逛)如果我将新项目添加到关联的集合中,我不必调用 foo.reload 来查看结果更改:
foo.bars
=> []
foo.bars_attributes = [{ :person_id => '288', :task_id => '1237' }]
=> [{ :person_id=>"288", :task_id=>"1237" }]
foo.save
=> true
foo.bars
=> [#<Bar id: 6, person_id: 288, task_id => 1237>]
了不起。但是当我从集合中删除一个项目时,我必须在更改(调用保存后在 DB 中可见)反映在我的集合中之前调用 foo.reload:
foo.bars_attributes = [{ :id => '6', :_delete => '1' }]
= [{ :_delete=>"1", :id=>"6" }]
foo.save
=> true
foo.bars
=> [#<Bar id: 6, person_id: 288, task_id => 1237>]
foo.reload
foo.bars
=> []
这是正常的,还是我在关联更新中做错了什么?