On 10/1/06, Sam Donaldson
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
>
> Hi,
>
> I''d like to understand how Rail''s handles rollbacks. Say
I have the
> following:
>
> def action(...)
>
> User.transaction do
> user1 = User.new
> user1.attr1 = 5
> relationship = Relationship.new
> if user1.save
> ...do stuff...
> end
>
> if relationship.save
> ...do stuff...
> end
> end
> end
>
> My question is, say user1.save did not go through due to some UNIQUE
> constraint violoation in the db, does Rails continue execution to
> relationship.save? Or does it throw an exception, and if so,
what''s the
> exception that it throws to tell us that it did a ROLLBACK. Since
I''m
> doing a user1.save without the ''user1.save!'', instead of
throwing an
> exception, it''ll return False. So if that''s the case,
does it just
> continue execution with relationship.save?
The database adapter would raise ActiveRecord::StatementInvalid. Your
transaction block would rescue the exception, rollback the db transaction,
and reraise the exception.
jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---