Forum: Ruby on Rails A question of style - save vs. 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.
D4e51fd9554030ab55c379fdc1a34826?d=identicon&s=25 Keith Lancaster (klancaster)
on 2006-04-24 21:23
A pseudo poll on AR error handling...

With AR, you have the choice between the following:

(1)

if thing.save
  go_for_it
else
  handle_the_error
end

(2)
begin
  thing.save!
rescue RecordInvalid => error
  handle_the_error
end

(3)
override rescue_action_in_public (ala the recipes book)
or do something homegrown, and then just

thing.save!

---
The questions are:
-Which idiom do you use and why?
-Are you consistent across your app/controllers, or do you mix and
match?
-Or do you just trust the gods of the Database and skip error handling
altogether?

Keith
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-04-25 00:36
(Received via mailing list)
Hello Keith !

2006/4/24, Keith Lancaster <klancaster1957@gmail.com>:
> -Which idiom do you use and why?

redirect_to() && return if model.save

> -Are you consistent across your app/controllers, or do you mix and
> match?

Depends on what I'm doing.  If the probability of having invalid data
is feeble (setting a flag on/off, changing a status), I'll use
model.save!, to ensure no invalid data is left hanging.

> -Or do you just trust the gods of the Database and skip error handling
> altogether?

Yikes, who would do that ?  Where do I put that chicken ?

Bye !
This topic is locked and can not be replied to.