更新嵌套属性集合后是否有必要调用 ActiveRecord::Base.reload ?

发布于 2024-08-16 07:57:25 字数 683 浏览 4 评论 0原文

我发现(在脚本/控制台中闲逛)如果我将新项目添加到关联的集合中,我不必调用 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>]

太棒了。但是,当我从集合中删除项目时,我必须在更改(调用保存后在数据库中可见)反映在我的集合中之前调用 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
=> []

这是正常的,还是我在关联中做了一些稍微错误的事情更新?

I am finding (noodling around in script/console) that if I add a new item to the associated collection, I don't have to call foo.reload to see the resulting change:

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>]

Terrific. But when I remove an item from the collection, I do have to call foo.reload before the change (visible in DB after call to save) is reflected in my collection:

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
=> []

Is this normal, or am I doing something slightly wrong in the association update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2024-08-23 07:57:25

我在 Rails 3.0 中看到了同样的事情(在您撰写本文时尚未发布)。它因需要最少数量的关联的验证而造成严重破坏。即使更新删除了所有子属性,父实例也会说它是有效的。

我设计了一种解决方法,通过使用以下方法在我的 Parent 模型中包含一个模块,并将其替换为控制器的 update 方法中的 update_attributes()

  def update_with_association_size_validations(attributes)
    update_successful = false
    self.class.transaction do
      self.update_attributes(attributes)
      self.reload
      update_successful = true if self.valid?
      raise ActiveRecord::Rollback unless update_successful
    end
    update_successful
  end

这绝不是一个完美的解决方案,并且尚未在更深层的嵌套关联上进行测试。

快一年了,让总比不让好!

I'm seeing the same thing in rails 3.0 (no yet released at the time of your writing). It was reaping havoc with a validation that required a minimum number of associations. The parent instance would say it was valid, even after the update had removed all the child attributes.

I devised a workaround by including a module in my Parent model with the following method, and substituting that for update_attributes() in the controller's update method:

  def update_with_association_size_validations(attributes)
    update_successful = false
    self.class.transaction do
      self.update_attributes(attributes)
      self.reload
      update_successful = true if self.valid?
      raise ActiveRecord::Rollback unless update_successful
    end
    update_successful
  end

This is by no means a perfect solution, and it's untested on more deeply nested associations.

Better let than never, almost a year to the day!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文