# Can someone clear up nested transactions?

**URL:** <https://www.ruby-forum.com/t/can-someone-clear-up-nested-transactions/105202>\
**Category:** Rails\
**Created:** [May 27, 2007, 3:28am UTC](https://www.ruby-forum.com/t/can-someone-clear-up-nested-transactions/105202 "2007-05-27T03:28:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 27, 2007, 3:28am UTC](https://www.ruby-forum.com/t/can-someone-clear-up-nested-transactions/105202/1 "2007-05-27T03:28:09Z")

</div>

I simply can’t find a straight answer on this anywhere. If I have the  
need of nested transactions do I need to do this

Article.transaction

my\_article = Article.new(:title =\> ‘This is a new title’, :content  
=\> ‘This is some new content’)  
my\_article.save

Comment.transaction

```
my_comment = Comment.new(:article => my_article, :body => 'This is

```

the body of my comment’)  
my\_comment.save

```
CommentConfirmation.transaction

  my_conf = CommentConfirmation.new(:commnet =>

```

my\_comment, :confirmed =\> true)  
my\_conf.save

```
end

```

end

Vote.transaction

```
my_vote = Vote.new(:article => my_article, :rank => 5)
my_vote.save

```

end

end

or does

Article.transaction

my\_article = Article.new(:title =\> ‘This is a new title’, :content  
=\> ‘This is some new content’)  
my\_article.save

my\_comment = Comment.new(:article =\> my\_article, :body =\> ‘This is  
the body of my comment’)  
my\_comment.save

my\_conf = CommentConfirmation.new(:commnet =\> my\_comment, :confirmed  
=\> true)  
my\_conf.save

my\_vote = Vote.new(:article =\> my\_article, :rank =\> 5)  
my\_vote.save

end

do the same thing?

Also, does create work the same as save, in terms of transactions, so  
if I do something like

Article.transaction

my\_article = Article.new(:title =\> ‘This is a new title’, :content  
=\> ‘This is some new content’)  
my\_article.save

Comment.transaction

```
my_comment = Comment.new(:article => my_article, :body => 'This is

```

the body of my comment’)  
my\_comment.save

end

end

is that the same as

Article.transaction

my\_article = Article.create(:title =\> ‘This is a new  
title’, :content =\> ‘This is some new content’)

Comment.transaction

```
my_comment = Comment.create(:article => my_article, :body => 'This

```

is the body of my comment’)

end

end

Dale

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 30, 2007, 6:02am UTC](https://www.ruby-forum.com/t/can-someone-clear-up-nested-transactions/105202/2 "2007-05-30T06:02:18Z")

</div>

The short answer to your question is yes, all the examples you gave  
above are equivalent: they would each roll back under the same  
cicumstances and leave the DB in the same condition. Nesting  
transactions in Rails _almost_ never has an effect. I was going to  
give a more complete explanation, but it got sort of unreadable so I  
posted it here:  
[http://expectedbehavior.com/articles/2007/05/29/nested-transactions-in-rails](http://expectedbehavior.com/articles/2007/05/29/nested-transactions-in-rails)

Hopefully that’s enough to clear things up for you.

Matt

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [June 26, 2007, 10:54am UTC](https://www.ruby-forum.com/t/can-someone-clear-up-nested-transactions/105202/3 "2007-06-26T10:54:56Z")

</div>

Hi,

how can I span a transaction over two or more unrelated tables?  
Is there a generic way to start a transaction on a not per-only-one-  
model basis, so that I can change rows in different tables and a  
rollback rolls back allchanges in all unrelated tables?  
So the sould would not be “MyModel.transaction…” but something like  
“transaction…”

thanks,  
Alex.
