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
on 2007-06-12 09:46
on 2007-06-12 10:08
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.
on 2007-06-12 10:16
So it means there is no way that I handle programmatically?
on 2007-06-13 10:12
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.
on 2007-06-13 10:38
What is about if I just handle all exception to make these 3 methods like: begin User.update(12, :email => "email@example.com") # or delete, create true rescue false end What do you think?
on 2007-06-14 10:42
Should work, and looks good. Try it =)