保存时忽略 before_update

发布于 2024-09-28 20:43:12 字数 254 浏览 2 评论 0原文

某些情况下我不想执行 before_update。请帮我。

情况 A:如果我想使用 before_update

obj = Object.find(id)
obj.save

但情况 BI 不想使用 before_update

obj = Object.find(id)
obj.save # in case I want used before_update

some case I don't want execute before_update. please help me.

case A: in case I want used before_update

obj = Object.find(id)
obj.save

but case B I don't want used before_update

obj = Object.find(id)
obj.save # in case I want used before_update

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

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

发布评论

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

评论(2

风吹过旳痕迹 2024-10-05 20:43:12

方法#save接受选项的哈希值。跳过验证:

obj.save(:validate => false)

这是使用公共 API 跳过验证的记录方法。不要尝试使用 send 来调用内部方法,否则您的应用程序将来可能无法工作。

The method #save accepts a Hash of option. To skip validations:

obj.save(:validate => false)

This is the documented way to skip validations using the public API. Don't try to use send to call internal methods or your application might not work in the future.

满天都是小星星 2024-10-05 20:43:12

update_without_callbacks 和 create_without_callbacks 是私有方法。这些方法不会调用任何回调。

obj = Object.find(id)
obj.send(:update_without_callbacks)

obj = Object.new(:name => 'foo')
obj.send(:create_without_callbacks)

update_without_callbacks and create_without_callbacks are private methods. These methods will not call any callbacks.

obj = Object.find(id)
obj.send(:update_without_callbacks)

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