# Update\_attributes on multiple models, revert back on error

**URL:** <https://www.ruby-forum.com/t/update-attributes-on-multiple-models-revert-back-on-error/157065>\
**Category:** Rails\
**Created:** [January 7, 2009, 12:54am UTC](https://www.ruby-forum.com/t/update-attributes-on-multiple-models-revert-back-on-error/157065 "2009-01-07T00:54:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terpinator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@Terpinator](https://www.ruby-forum.com/u/Terpinator)\
**Post date:** [January 7, 2009, 12:54am UTC](https://www.ruby-forum.com/t/update-attributes-on-multiple-models-revert-back-on-error/157065/1 "2009-01-07T00:54:40Z")

</div>

Hi,

I have one method, ‘updateStuff’, that will call ‘update\_attributes’  
on 3 different models in the DB…

How can I account for validation to revert back all updates if one of  
the models is not valid?

I’ve looked into Model.valid? which doesn’t seem to work for me …

Any idea?

Thanks

---

<div class="post-metadata">

**Author:** ![Terpinator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@Terpinator](https://www.ruby-forum.com/u/Terpinator)\
**Post date:** [January 7, 2009, 12:53pm UTC](https://www.ruby-forum.com/t/update-attributes-on-multiple-models-revert-back-on-error/157065/2 "2009-01-07T12:53:17Z")

</div>

Terpinator wrote:

> I have one method, ‘updateStuff’, that will call ‘update\_attributes’  
> on 3 different models in the DB…
> 
> How can I account for validation to revert back all updates if one of  
> the models is not valid?

Note that you want all-or-nothing DB updates, but, for purposes  
of form redisplay, always want both in-memory attribute updates  
and validity-checking done on each. Here’s how:

1. Update attributes on each without saving using the attributes=  
method.
2. Call valid? on each, storing each result in a local variable.
3. Proceed with transaction-wrapped saves if all three variables are  
true.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![Terpinator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@Terpinator](https://www.ruby-forum.com/u/Terpinator)\
**Post date:** [January 7, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/update-attributes-on-multiple-models-revert-back-on-error/157065/3 "2009-01-07T14:24:03Z")

</div>

that’s what i would do, too. but you said valid? is not working for  
you. why is that? (where seems to be the problem?)
