且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails:after_create 中的异常停止保存

更新时间:2023-01-26 19:18:19

是的,回调都包含在一个事务中.

Yes, the callbacks are all wrapped up in a transaction.

基本上,以下会导致回滚:

Basically, the following will cause a rollback:

  • before_save 或类似回调返回 false
  • before_save 或类似回调中的异常
  • after_save 或类似回调(after_create)中的异常
  • return false from before_save or similar callbacks
  • exception in before_save or similar callbacks
  • exception in after_save or similar callbacks (after_create)

以下不会导致回滚:

  • after_save 或类似回调返回 false
  • after_commit 中的异常
  • return false from after_save or similar callbacks
  • exception in after_commit

如果您不希望异常导致回滚,请使用 after_commit

If you don't want an exception to cause a rollback, use after_commit