Well, lets say the first save is OK, but the second save results in a
validation error.
Then the second save is rolled back, obviously, but then you want the whole
thing to roll back.
I''m talking about an actual DB transaction rollback.
I realise that the "illegal code" example that I included shows only a
transaction connected to the one model.
In general however, I''d like to start a block, which also starts a
transaction in the DB, do some stuff on one or more model objects, save them
to the DB, but if some condition should occur, be able to rollback all DB
changes in the block.
I guess this is not easy to do in a general framework, since perhaps not all
DBMSs has support for transactions.
(I work with Sybase ASE which does have this capability.)
Best regards,
-Rolf
On Fri, Apr 16, 2010 at 4:36 PM, Me
<chabgood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Why save and then rollback? AR will rollback if validations fail.
>
> On Apr 16, 3:54 am, Rolf Pedersen
<rolf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Hi
> >
> > Not trying to solve a specific problem here, but have a theoretical
> > question, anyhow...
> > Having created a simple model/controller with the scaffold generator,
I
> > created a simple remote client based on ActiveResource.
> > A part of this code is illegal, but illustrates what I want to
> accomplish:
> >
> > require ''active_resource''
> >
> > class EnvironmentType < ActiveResource::Base
> > self.site = "http://xxx:3001"
> > end
> >
> > etypes = EnvironmentType.find(:all)
> > puts etypes.map(&:name)
> >
> > # This part of the code is not legal, based on some code I found for
> > ActiveRecord
> > EnvironmentType.transaction do
> > etypes[0].name = "Prod"
> > etypes[0].save
> > etypes[1].name = "Testing"
> > etypes[1].save
> > raise ActiveRecord::Rollback
> > end
> > # End of illegal part...
> >
> > etypes = EnvironmentType.find(:all)
> > puts etypes.map(&:name)
> >
> > # Assumption is that that the names has not changed at this point...
> >
> > Basically, my question is if there is some (easy) way to initiate a
> > transaction from this remote client with the possibility of a rollback
> after
> > a set of saves has been done.
> > OK, so maybe the best/shortest answer is that this is bad design to
> remotely
> > initiate a transaction like this, but I''m gonna ask anyway
:o)
> >
> > Best regards,
> > Rolf
> >
> > --
> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> > For more options, visit this group athttp://
> groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.