1

我的模型中定义了以下after_save回调。

after_save :validate_image, :publish, :update_some_data, :send_notifications

after_save是否可以根据某些条件跳过其余的回调validate_image,所以像这样 -

def validate_image
  if image_not_valid
    # skip rest of the callbacks in the after_save chain
    destroy # destroy this record
  end
end

注意:- 我也在根据该条件销毁记录,并且我有一些需要执行的“after_destroy”回调。

我正在使用带有 Mongoid v2.4.10 的 Rails v3.2.6,但我想如果它是 ActiveRecord 不会有什么不同。

4

2 回答 2

0

像这样更改您的 before_save 定义

after_save do
   validate_image
   unless image_not_valid
       publish 
       update_some_data 
       send_notifications
   end
end
于 2012-07-04T04:43:39.333 回答
0

使用 ActiveRecord:

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

如果 before_* 回调返回 false,则所有后面的回调和相关操作都将被取消。如果 after_* 回调返回 false,则取消所有后面的回调

使用常规回调,只有 before_* 回调一旦返回 false 就会停止

于 2012-07-12T00:58:48.173 回答