On 11/23/06, Pratik <pratiknaik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I have like 5-7 models in nested transactions like,
>
> A.transaction do
> B.transaction do
> C...
> ...
> end
> end
>
> It''s mainly because I have to create has_many associated objects
when
> I create a new object of A. Is this the best way to do it ?
You don''t need to do nested transactions in Rails. Calling
A.transaction is the same as calling B.transaction - it actually just
goes to AR::Base.transaction, with nothing special on the table.
Calling AR::Base.transaction is what starts the database transaction.
What you''re probably interested in is also ensuring the integrity of
the objects, instead of merely the database. In that case, pass the
objects into the transaction method:
A.transaction(object_1, object_2, object_3) do
...
end
If the transaction fails, then the database won''t be updated (of
course), and object_1/2/3 will all revert to their state before the
transaction.
Pat
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---