3 ms·
It's not really a gotcha since it's documented: See the section "Cancelling Callbacks" in the docs for ActiveRecord::Callback: http://rails.rubyonrails.org/cla
by technoweenie 16y ago
It's not really a gotcha since it's documented:
See the section "Cancelling Callbacks" in the docs for ActiveRecord::Callback: http://rails.rubyonrails.org/classes/ActiveRecord/Callbacks.html http://rails.rubyonrails.org/classes/ActiveRecord/Callbacks....
"If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last."
before_filter in controllers used to work the same way, but somewhere along the way it was changed to check #performed? on the controller. That way before_filters only cancel the action if it redirects or renders a template.
How else would you do that for callbacks though? Force the user to raise something like ActiveRecord::CancelCallback? Use the obscure throw/catch statements?
- Robin_Message 16y agoRaising an exception is a good method. A better method would be a condition system [1], which enables more fine grained handling of such things. throw and catch are a kind of bad implementation of this, but there is no reason you couldn't use them, except that very few people do. In practice, maybe a Ruby linter could/should pick this pattern up and warn about it? It wouldn't be hard to identify functions which return a value based on a block if with no else say. [1] http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html http://www.gigamonkeys.com/book/beyond-exception-handling-co...