Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Cancel destroy on before_destroy callback

is there any way, given certain condition, cancel the destroy of an object on the before_destroy callback of active record? Thanks

like image 490
Ronan Lopes Avatar asked Jun 14 '16 19:06

Ronan Lopes


1 Answers

You should return false.

Rails 5

"Canceling callbacks

If a before_* callback throws :abort, all the later callbacks and the associated action are cancelled."

Rails 4 and lower

"Canceling callbacks

If a before_* callback returns false, all the later callbacks and the associated action 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."

Source

like image 53
Leonel Galán Avatar answered Sep 20 '22 15:09

Leonel Galán