Forum: Ruby on Rails ActiveResource and transactions

819f804405aa2d98d5491466ded25521?d=identicon&s=25 Rolf Pedersen (Guest)
on 2010-04-16 10:55
(Received via mailing list)
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
8133ee52ca72188132b5c7d97dfb967f?d=identicon&s=25 Chris Habgood (chabgood)
on 2010-04-16 16:39
(Received via mailing list)
Why save and then rollback?  AR will rollback if validations fail.
819f804405aa2d98d5491466ded25521?d=identicon&s=25 Rolf Pedersen (Guest)
on 2010-04-16 17:28
(Received via mailing list)
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
E5469d44aba79d02f1d1cd75f3b5a471?d=identicon&s=25 Evaldo Kalbermatter (evaldo)
on 2014-04-14 21:37
Rolf Pedersen wrote in post #905743:
> 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

Rolf, i have the same question.

You solve this?

Thanks in advance, Evaldo.
15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2014-04-14 22:37
(Received via mailing list)
On Apr 14, 2014, at 1:37 PM, Evaldo Kalbermatter <lists@ruby-forum.com>
wrote:

> Rolf Pedersen wrote in post #905743:
>> 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.
...
>> Best regards,
>> -Rolf
>
> Rolf, i have the same question.
>
> You solve this?

google "rails transaction"


--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
E5469d44aba79d02f1d1cd75f3b5a471?d=identicon&s=25 Evaldo Kalbermatter (evaldo)
on 2014-04-15 04:50
Scott Ribe wrote in post #1143072:
> On Apr 14, 2014, at 1:37 PM, Evaldo Kalbermatter <lists@ruby-forum.com>
> wrote:
>
>> Rolf Pedersen wrote in post #905743:
>>> 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.
> ...
>>> Best regards,
>>> -Rolf
>>
>> Rolf, i have the same question.
>>
>> You solve this?
>
> google "rails transaction"
>
>
> --
> Scott Ribe
> scott_ribe@elevated-dev.com
> http://www.elevated-dev.com/
> (303) 722-0567 voice

You have to read better :)

We're talking about ActiveResource. When the post went to the server
think it is not rollback.

Just was curious. It is something to try and understand how it works.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.