# How to handle all errors programmatically on saving, updatin

**URL:** <https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595>\
**Category:** Rails\
**Created:** [June 12, 2007, 9:46am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595 "2007-06-12T09:46:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 12, 2007, 9:46am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595/1 "2007-06-12T09:46:55Z")

</div>

Hello guys,

I want to know how to handle all errors programmatically when I save,  
update, and delete on record. For the save method, it will return true  
or false, but for update method and destroy/delete method they seems  
not to return any valid information they work successfully or not.  
Appreciate all your idea…

Thanks  
Chamnap

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 12, 2007, 10:08am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595/2 "2007-06-12T10:08:00Z")

</div>

Destroy doesn’t return true or false afaik, it just instantiates the  
objects and destroys them, calling all the callbacks.

Update does a find on the id you pass to it, and saves it. If the save  
fails, the unsaved object is returned, so I guess you can check wether  
or not the object changed if you want to figure out if the update was  
a success or not.

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 12, 2007, 10:16am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595/3 "2007-06-12T10:16:03Z")

</div>

So it means there is no way that I handle programmatically?

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 13, 2007, 10:12am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595/4 "2007-06-13T10:12:22Z")

</div>

Of course there is. For update, for instance, the unsaved object is  
returned, so you can probably do “if myoldobject.eql? mynewobject” for  
instance.

And for destroy - if it didn’t raise anything, it worked.

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 14, 2007, 10:42am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595/5 "2007-06-14T10:42:50Z")

</div>

Should work, and looks good. Try it =)

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [June 13, 2007, 10:38am UTC](https://www.ruby-forum.com/t/how-to-handle-all-errors-programmatically-on-saving-updatin/106595/6 "2007-06-13T10:38:24Z")

</div>

What is about if I just handle all exception to make these 3 methods  
like:

begin  
User.update(12, :email =\> “removed\_email\_address@domain.invalid”) # or delete, create  
true  
rescue  
false  
end

What do you think?
