Rails 4: Skip callback

前端 未结 4 1575
清酒与你
清酒与你 2021-01-17 21:16

I have an auction and a bid object in my application, when someone presses the BID BUTTON it then calls the BID CREATE controller which cre

4条回答
  •  日久生厌
    2021-01-17 21:43

    skip_callback is a complicated and not granular option.

    I prefer to use an attr_accessor:

    attr_accessor :skip_my_method, :skip_my_method_2
    after_save{ my_method unless skip_my_method }
    after_save{ my_method_2 unless skip_my_method_2 }
    

    That way you can be declarative when skipping a callback:

    model.create skip_my_method: true # skips my_method
    model.create skip_my_method_2: true # skips my_method_2
    

提交回复
热议问题