Forum: Ruby on Rails How do you detect if ActiveRecord 'update' fails?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Justin R. (Guest)
on 2005-12-27 14:41
(Received via mailing list)
So I'm updating multiple model objects in one go with something like
this in my controller:
=>	Photo.update(params[:photo].keys, params[:photo].values)

When the update fails due to validation errors, how do I detect it?
The Rails API says:
"If the save fails under validations, the unsaved object is still
So I can't simply do this:
  =>	if Photo.update(params[:photo].keys, params[:photo].values)

Any suggestions?
Rick O. (Guest)
on 2005-12-27 15:17
(Received via mailing list)
On 12/27/05, Justin R. <removed_email_address@domain.invalid> wrote:
> Any suggestions?
@photo = Photo.find(params[:id])
if @photo.update_attributes(params[:photo])...

Justin R. (Guest)
on 2005-12-27 15:47
(Received via mailing list)
"@photo = Photo.find(params[:id])
if @photo.update_attributes(params[:photo])..."

At first glance, that seems great for updating ONE model instance.
But I'm updating multiple objects. Are you suggesting that I should
loop through each of the key-values and check that way, like so:

for  params[:photo].each do |id|
	@photo= Photo.find(id)
	if @photo.update_attributes(params[:photo].values[id])

? (Sorry for the poor code, but you know what I'm getting at, right?)
Rick O. (Guest)
on 2005-12-27 17:00
(Received via mailing list)
On 12/27/05, Justin R. <removed_email_address@domain.invalid> wrote:
>         if @photo.update_attributes(params[:photo].values[id])
>                 ...
>         end
> end
> ? (Sorry for the poor code, but you know what I'm getting at, right?)

Well, I seem to be getting your requirements in pieces, so it's hard
figuring out how to phrase this :)

For instance, do you want the whole save operation to stop?  If so,
use save! and a transaction  I'm not sure how you have your hash
structured, but I'm guessing it's like:

params[:photo] = {
 1 => { photo_params },
 2 => { photo_params }...

def foo
  Photo.transaction do
    params[:photo].each do |id, photo_attributes|
      @photo = Photo.find(id)
      @photo.attributes = photo_attributes!
  error handling...

When save! fails due to a validation error, it raises an exception and
rolls back the transaction.

Oh, the simple solution is probably to just loop through the photos
after calling Photo.update and checking photos[i].errors.empty?

Justin R. (Guest)
on 2005-12-27 17:39
(Received via mailing list)
Sweet, thanks Rick. That certainly answers my question and confirms
that I gotta go about it the 'hard' way. Wouldn't it be nice if
ActiveRecord 'update' returned some kind of indication of errors if
any of its attempted 'saves' fail? (Nice bit about that
ActiveRecord.errors.empty?, very useful.) Ah well...
This topic is locked and can not be replied to.