Forum: Ruby on Rails Can someone clear up nested transactions?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
PeteSalty (Guest)
on 2007-05-27 05:28
(Received via mailing list)
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
Gordon (Guest)
on 2007-05-30 08:02
(Received via mailing list)
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/ne...

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

Matt
Alex Greif (Guest)
on 2007-06-26 12:54
(Received via mailing list)
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.
This topic is locked and can not be replied to.