2017-10-04 30 views
0
class A 
    after_commit :a, :b, :c, :d, :e, on: [:create, :update] 

    def a 
    end 

    def b 
    end 

    def c 
    end 

    def d 
    end 

    def e 
    end 

end 

现在after_commit会按顺序执行e,d,c,b,a。Rails:抢救after_commit回调方法失败并继续执行其他方法

有什么办法可以跳过一个方法,如果它引发了一些错误/异常,以便其他回调过滤器会尝试,即使方法执行失败之前执行。

+1

你尝试过什么?您是否已经通过'begin使用常规ol'异常处理? rescue'? – ddubs

+0

在真实代码中,回调中有很多方法。只是想知道是否有任何简单的黑客做同样的事情,而不是添加开始;每种方法内部的救援。 – Raj

回答

0

可以rescue任何错误:

class A 
    after_commit :a, :b, on: [:create, :update] 

    def a 
    # ... 
    rescue StandardError 
    # no-op 
    end 

    def b 
    # ... 
    rescue StandardError 
    # no-op 
    end 
end