# Catch validation errors from Model.update(params\[:object\].keys, params\[:object\].values)

**URL:** <https://www.ruby-forum.com/t/catch-validation-errors-from-model-update-params-object-keys-params-object-values/163814>\
**Category:** Rails\
**Created:** [March 25, 2009, 4:36am UTC](https://www.ruby-forum.com/t/catch-validation-errors-from-model-update-params-object-keys-params-object-values/163814 "2009-03-25T04:36:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 25, 2009, 4:36am UTC](https://www.ruby-forum.com/t/catch-validation-errors-from-model-update-params-object-keys-params-object-values/163814/1 "2009-03-25T04:36:03Z")

</div>

Hi there

I’m trying to update three records at a time with params coming back  
from an HTML form. The form is set up fine and the params hash is  
coming back in a form ready to use:

Model.update(params[:object].keys, params[:object].values)

However, I can’t catch the validation errors if the objects aren’t  
updated.

In my controller I have this:

if Discount.update(params[:discounts].keys, params  
[:discounts].values)  
flash[:notice\_good] = “Discounts have been updated”  
redirect\_to :action =\> ‘index’  
else  
flash[:notice\_bad] = “Couldn’t update discounts”  
redirect\_to :action =\> ‘index’ and return false  
end

I have \<%= error\_messages\_for :discount %\> in my view but can’t seem  
to display any validation errors.

Furthermore, it seems that my condtional “if Discount.update […]  
etc” fails if there are any errors in saving the updated data to the  
database.

Any clues on how to properly use Model.update with a params hash would  
be appreciated.

Richard

---

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 25, 2009, 8:10am UTC](https://www.ruby-forum.com/t/catch-validation-errors-from-model-update-params-object-keys-params-object-values/163814/2 "2009-03-25T08:10:44Z")

</div>

On 25 Mar., 04:35, bongoman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have \<%= error\_messages\_for :discount %\> in my view but can’t seem  
> to display any validation errors.
> 
> Furthermore, it seems that my condtional “if Discount.update […]  
> etc” fails if there are any errors in saving the updated data to the  
> database.
> 
> Any clues on how to properly use Model.update with a params hash would  
> be appreciated.

I don’t see why this should be working at all. If you take a look at  
the documentation for ActiveRecord::Base.update (http://  
[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002215)) it takes  
two arguments. id and attributes, so you should really be calling the  
method like this:

if Discount.update(params[:id], params[:discounts])  
flash[:notice\_good] = “Discounts have been updated”  
redirect\_to :action =\> ‘index’  
else  
flash[:notice\_bad] = “Couldn’t update discounts”  
redirect\_to :action =\> ‘index’ and return false  
end

Furthermore, you need to save the model in an instance variable in  
order to display any error messages, _and_ error messages will not be  
saved from request to request, so you need to do a render instead of a  
redirect:

@discount = Discount.find(params[:id])  
if @discount.update(params[:discount])  
flash[:notice] = “Discounts have been updated”  
redirect\_to :action =\> “index”  
else  
render :action =\> “new”  
end

Put \<%= error\_messages\_for :discount %\> in new.html.erb.

I can recommend the book “Agile Web D. with Rails” from  
Pragmatic Programmers to get started with Rails and get the hang of  
all the concepts of the framework.

–  
Best regards,  
David K.  
[http://twitter.com/rubyguy](http://twitter.com/rubyguy)

---

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 25, 2009, 10:11am UTC](https://www.ruby-forum.com/t/catch-validation-errors-from-model-update-params-object-keys-params-object-values/163814/3 "2009-03-25T10:11:19Z")

</div>

On Mar 25, 7:08 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Model.update(params[:object].keys, params[:object].values)  
> > else
> 
> > Any clues on how to properly use Model.update with a params hash would  
> > be appreciated.
> 
> I don’t see why this should be working at all. If you take a look at  
> the documentation for ActiveRecord::Base.update (http://  
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002215)) it takes  
> two arguments. id and attributes, so you should really be calling the  
> method like this:

Read the docs again. It can update multiple objects and there;s even  
an example in the docs.  
update will return an array of the activerecord objects that it tried  
to update, you can iterate over those and display errors for them, eg

error\_message\_for ‘discount’, :object =\>  
some\_array\_of\_active\_recored\_objects

You don’t want to redirect though.

Fred
