Rolling Back Transactions

Hey All,

Am I wrong in thinking that the id and new_record? attributes should be reverted if a transaction is rolled back? They don't currently revert so the id attribute is left with a value that isn't actually in the database and new_record? is returning false even though it should be true.

Here's a test demonstrating what I'm talking about:

   def test_should_reset_id_and_new_record_if_the_transaction_rolls_back
     topic_count_before_save = Topic.count

     new_topic = Topic.new(
       :title => "A new topic",
       :author_name => "Ben",
       :author_email_address => "ben@example.com",
       :written_on => "2003-07-16t15:28:11.2233+01:00",
       :last_read => "2004-04-15",
       :bonus_time => "2005-01-30t15:28:00.00+01:00",
       :content => "Have a nice day",
       :approved => false)

     new_record_snapshot = new_topic.new_record?
     id_present = new_topic.has_attribute?(Topic.primary_key)
     id_snapshot = new_topic.id

     Topic.transaction do
       new_topic.save
       raise ActiveRecord::Rollback
     end

     topic_count_after_save = Topic.count

     assert_equal topic_count_before_save, topic_count_after_save
     assert_equal new_record_snapshot, new_topic.new_record?, "The topic should have its old new_record value"
     assert_equal id_snapshot, new_topic.id, "The topic should have its old id"
     assert_equal id_present, new_topic.has_attribute?(Topic.primary_key)
   end

Thanks,

Jonathan

You are describing object transactions, Rails used to have support#
for those, but it was removed [1] and is currently available
as a plugin [2].

[1] http://dev.rubyonrails.org/ticket/5637
[2] http://code.bitsweat.net/svn/object_transactions/

This was actually not removed. See http://dev.rubyonrails.org/changeset/5830.
I've submitted a bug report about this: http://dev.rubyonrails.org/ticket/9105

This was actually not removed. See http://dev.rubyonrails.org/changeset/5830.
I've submitted a bug report about this: http://dev.rubyonrails.org/ticket/9105

Figuring out what objects were modified in a block is well beyond the
scope of what we're going to fix in 2.0. Rolling back the state from
a particular instance is one thing, but extending that is very
difficult with AR's approach to persistence.