Rails 4: Skip callback

前端 未结 4 1537
清酒与你
清酒与你 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:34

    ActiveSupport::Callbacks::ClassMethods#skip_callback is not threadsafe, it will remove callback-methods for time till it is being executed and hence and another thread at same time cannot get the callback-methods for execution.

    Look at the informative post by Allerin - SAVE AN OBJECT SKIPPING CALLBACKS IN RAILS APPLICATION

    0 讨论(0)
  • 2021-01-17 21:37

    You can try skipping callback with skip_callback

    http://www.rubydoc.info/docs/rails/4.0.0/ActiveSupport/Callbacks/ClassMethods:skip_callback

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-17 21:53

    You can use update_columns See this http://edgeguides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks

    Is there any specific condition like when you don't have endtime then only you need to set end time if that the you can do

    def set_endtime 
       if endtime.nil? 
         self.endtime=self.starttime+self.auctiontimer 
       end 
    end 
    

    OR

    before_update :set_endtime if: Proc.new { |obj| obj.endtime.nil? }
    
    0 讨论(0)
提交回复
热议问题