Forum: Ruby on Rails Stopping a save from the before_save

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8fd9f26b8f23d49c8a89b63f1279e24f?d=identicon&s=25 Chris Nolan.ca (Guest)
on 2006-01-03 08:56
(Received via mailing list)
What's the better thing to do in a before_save if you don't want the
save to continue?

raise an exception, or do an errors.add or both?

Thanks,
Chris Nolan.ca
http://kweschun.com/ - Do you have a Kweschun?
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2006-01-03 10:33
(Received via mailing list)
On 3.1.2006, at 9.56, Chris Nolan.ca wrote:

> What's the better thing to do in a before_save if you don't want
> the save to continue?

Return false.

"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." [1]

//jarkko

[1] http://api.rubyonrails.com/classes/ActiveRecord/Ca...
37b7b848b1e3d667174d28d32d04bcee?d=identicon&s=25 Charles M. Gerungan (Guest)
on 2006-01-03 11:19
(Received via mailing list)
On 3-jan-2006, at 10:32, Jarkko Laine wrote:

>> What's the better thing to do in a before_save if you don't want
>> the save to continue?
>
> Return false.
>
> "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.

Does that mean that raising an exception in a callback doesn't cancel
later callbacks?

When returning false, how do you detect which callback return false?

--
Regards, Charles.
This topic is locked and can not be replied to.