Class User before_save :set_searchable def set_searchable self.searchable = true if self.status == :active end end >> u = User.last >> u.save false
u.save always return false. If i remove the before_save it works also if i give a return true in before_save it works
so do i need to give return statements in before_save ? will ActiveRecord saves an object if the before_save returns false ?
Where can i see a full documentation regarding callbacks and its workflow .
Thanks in advance
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
In Rails, callbacks are hooks provided by Active Record that allow methods to run before or after a create, update, or destroy action occurs to an object. Since it can be hard to remember all of them and what they do, here is a quick reference for all current Rails 5 Active Record callbacks.
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
From: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
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.
So, yes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With