# Using create, update and validation

**URL:** <https://www.ruby-forum.com/t/using-create-update-and-validation/134397>\
**Category:** Rails\
**Created:** [April 10, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397 "2008-04-10T14:29:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/1 "2008-04-10T14:29:38Z")

</div>

Hi List

When using the create and update methods, is there anyway of confirming  
that the create/update method was unsuccessful- e.g. if it failed  
validation? The API documentation and playing around in the console  
suggest that it fails silently.

With a create, I could always check if the object’s id has been set -  
but what would one do on an update?

Any help appreciated

Rory

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/2 "2008-04-10T14:35:02Z")

</div>

On 10 Apr 2008, at 13:28, Rory McKinley wrote:

> but what would one do on an update?  
> They return true/false. the ! variants raise an exception instead. You  
> can always call the valid? method to see if an object’s validations  
> pass.

Fred

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/3 "2008-04-10T14:38:49Z")

</div>

simply check if save or update\_attributes returns true:

if @alert.save  
flash[:notice] = ‘cool, its ok’  
redirect\_to wherever  
else  
flash[:notice] = ‘oops!’  
end

same for update\_attributes  
and of course you can use render :template =\>  
instead of redirect or redirect in case of errors

another way would be to check the errors object of the record

if @alert.errors.size \> 0 …  
the error object is handy anyway, since it will contain all those errors  
from your validations

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/4 "2008-04-10T14:45:47Z")

</div>

Thorsten M. wrote:

> if @alert.errors.size \> 0 …  
> the error object is handy anyway, since it will contain all those errors  
> from your validations

Thanks Thorsten

So , would it be recommended to use this method (checking @alert.errors)  
versus catching an exception from the save! or create! or update! ?

Thanks

Rory

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/5 "2008-04-10T14:56:11Z")

</div>

> So , would it be recommended to use this method (checking @alert.errors)  
> versus catching an exception from the save! or create! or update! ?

we mostly use the “if @alert.save then” version  
Freds way with the exceptions is good too of course

just a matter of taste (or maybe Fred can enlighten us, if it has  
additional advantages). i just like to handle errors where they happen  
to be and with the rescue handling for exceptions you end up with a bit  
more code, i think

i mentioned the errors object mainly because it’s another alternative  
and useful for other purposes (show errors to the user, logger.info  
errors for debugging)

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/6 "2008-04-10T14:59:58Z")

</div>

Thorsten M. wrote:

> if @alert.errors.size \> 0 …  
> the error object is handy anyway, since it will contain all those errors  
> from your validations

One more question 😉 - is there anyway that the method could have  
completed and produced errors? Or is errors.size \> 1 a guarantee that  
the operation failed?

R

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/using-create-update-and-validation/134397/7 "2008-04-10T14:42:13Z")

</div>

Frederick C. wrote:

> On 10 Apr 2008, at 13:28, Rory McKinley wrote:
> 
> > With a create, I could always check if the object’s id has been set -  
> > but what would one do on an update?
> 
> They return true/false. the ! variants raise an exception instead. You  
> can always call the valid? method to see if an object’s validations  
> pass.
> 
> Fred  
>   
> Hi Fred

Thanks for that - wasn’t aware there is a create! and update!. Just a  
note - according to the api docs:

create(attributes = nil)

Creates an object (or multiple objects) and saves it to the database, if  
validations pass. The resulting object is returned whether the object  
was saved successfully to the database or not.

But I will just catch the exception from create! and update!.

Thanks for the help
