Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: Skip callback

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 created the bid, and then does some other things on the auction object:

BIDS CONTROLLER -> CREATE

@auction.endtime += @auction.auctiontimer
@auction.winner = @auction.arewinning 
@auction.save

AUCTION MODEL

before_update :set_endtime

def set_endtime
   self.endtime=self.starttime+self.auctiontimer
end

So the question is: How can C skip the "before callback" only, in this specific @auction.save

like image 480
Crazy Barney Avatar asked Oct 18 '13 11:10

Crazy Barney


2 Answers

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
like image 105
sites Avatar answered Sep 18 '22 15:09

sites


You can try skipping callback with skip_callback

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

like image 29
akusy Avatar answered Sep 20 '22 15:09

akusy