Rails:检查模型是否确实保存在 after_save 中

发布于 2024-08-19 23:04:30 字数 349 浏览 9 评论 0原文

即使模型没有更改并且没有生成插入/更新查询,ActiveRecord 也会在每次调用 save 方法时调用 after_save 回调。

这实际上是默认行为。在大多数情况下这是可以的。

但是一些 after_save 回调对模型是否实际保存很敏感。

有没有办法确定模型是否确实保存在 after_save 中?

我正在运行以下测试代码:

class Stage < ActiveRecord::Base
  after_save do
    pp changes
  end
end

s = Stage.first
s.name = "q1"
s.save!

ActiveRecord use to call after_save callback each time save method is called even if the model was not changed and no insert/update query spawned.

This is the default behaviour actually. And that is ok in most cases.

But some of the after_save callbacks are sensitive to the thing that if the model was actually saved or not.

Is there a way to determine if the model was actually saved in the after_save?

I am running the following test code:

class Stage < ActiveRecord::Base
  after_save do
    pp changes
  end
end

s = Stage.first
s.name = "q1"
s.save!

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

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

发布评论

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

评论(3

伊面 2024-08-26 23:04:30

ActiveRecord用来调用after_save
每次保存方法的回调是
即使模型没有被调用
已更改并且没有插入/更新查询
产生。

每次成功保存记录时,无论更改如何,ActiveRecord 都会执行 :after_save 回调。

# record invalid, after_save not triggered
Record.new.save

# record valid, after_save triggered
r = Record.new(:attr => value)

# record valid and not changed, after_save triggered
r.save

您想知道的是记录是否更改,而不是记录是否保存。
您可以使用 record.changed? 轻松完成此操作

class Record

  after_save :do_something_if_changed

  protected

  def do_something_if_changed
    if changed?
      # ...
    end
  end
end

ActiveRecord use to call after_save
callback each time save method is
called even if the model was not
changed and no insert/update query
spawned.

ActiveRecord executes :after_save callbacks each time the record is successfully saved regardless it was changed.

# record invalid, after_save not triggered
Record.new.save

# record valid, after_save triggered
r = Record.new(:attr => value)

# record valid and not changed, after_save triggered
r.save

What you want to know is if the record is changed, not if the record is saved.
You can easily accomplish this using record.changed?

class Record

  after_save :do_something_if_changed

  protected

  def do_something_if_changed
    if changed?
      # ...
    end
  end
end
私藏温柔 2024-08-26 23:04:30

保存后,检查保存的对象是否是新对象

a = ModelName.new
a.id = 1
a.save
a.new_record?
#=> false

After a save, check to see if the object saved is a new object

a = ModelName.new
a.id = 1
a.save
a.new_record?
#=> false
瀞厅☆埖开 2024-08-26 23:04:30

对于登陆这里的其他人,最新的 Rails 5 有一个模型方法 saved_changes? 和一个相应的方法 saved_changes 显示这些更改。

book = Book.first
book.saved_changes?
=> false

book.title = "new"
book.save

book.saved_changes?
=> true
book.saved_changes
=> {"title" => ["old", "new"]},

For anyone else landing here, latest rails 5 has a model method saved_changes? and a corresponding method saved_changes which shows those changes.

book = Book.first
book.saved_changes?
=> false

book.title = "new"
book.save

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