ruby-on-rails 从Rails 4.2升级到5.2,在回调中实现throw(:abort)会引发Mongoid验证异常,如何防止?

pu3pd22g  于 2023-07-01  发布在  Ruby
关注(0)|答案(1)|浏览(114)

我正在做的升级是

  • Ruby 2.6
  • MongoDB 3.6
  • Mongoid从5.0到7.1
  • Rails从4.2到5.2

我有一个回调,在一定条件下停止未来的回调。现在我必须使用throw(:abort)来停止回调并阻止save操作,Mongoid将抛出:

Mongoid::Errors::Callback:

       message:
         Calling save! on Survey resulted in a false return from a callback.
       summary:
         If a before callback returns false when using Document.create!, Document#save!, or Document#update_attributes! this error will get raised since the document did not actually get saved.
       resolution:
         Double check all before callbacks to make sure they are not unintentionally returning false.

这是有问题的。我们甚至不期望一个错误消息,这是记录,我们继续前进。我们只想让record.save返回false。如何从Mongoid中抑制这个异常?

t1qtbnec

t1qtbnec1#

问题是我抛出了false,同时也返回了false。throw(:abort)。

相关问题