Forum: Ruby on Rails Update_attributes on multiple models, revert back on error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B567bf264f73fe8e27b30efb61f41673?d=identicon&s=25 Terpinator (Guest)
on 2009-01-07 00:54
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-01-07 12:53
(Received via mailing list)
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-01-07 14:24
(Received via mailing list)
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?)
This topic is locked and can not be replied to.