aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-08-11 19:08:16 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-08-11 23:24:47 -0300
commit786713a3abc375fab4d5c2f6f62a878c2ab51022 (patch)
tree70035fe7fa1d94d9e3e92726533e9a34c2570959 /activerecord
parent20f18cbc148270112c670189cf5489900a2271dc (diff)
downloadrails-786713a3abc375fab4d5c2f6f62a878c2ab51022.tar.gz
rails-786713a3abc375fab4d5c2f6f62a878c2ab51022.tar.bz2
rails-786713a3abc375fab4d5c2f6f62a878c2ab51022.zip
Merge pull request #6073 from daveyeu/restore-state-on-record-invalid
Restore state on create when ActiveRecord::RecordInvalid is raised Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/transactions.rb2
-rw-r--r--activerecord/test/cases/transactions_test.rb17
3 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index c7775870a9..18d7871a82 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.9 (unreleased)
+* Fix AR#create to return an unsaved record when AR::RecordInvalid is
+ raised. Fixes #3217.
+
+ *Dave Yeu*
+
* Remove unnecessary transaction when assigning has_one associations with a nil or equal value.
Fix #7191.
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 8c93f50926..98569aa307 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -325,7 +325,7 @@ module ActiveRecord
def restore_transaction_record_state(force = false) #:nodoc:
if defined?(@_start_transaction_state)
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
- if @_start_transaction_state[:level] < 1
+ if @_start_transaction_state[:level] < 1 || force
restore_state = remove_instance_variable(:@_start_transaction_state)
was_frozen = @attributes.frozen?
@attributes = @attributes.dup if was_frozen
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index 3efaaef7de..c2fc239167 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -204,6 +204,23 @@ class TransactionTest < ActiveRecord::TestCase
end
end
+ def test_callback_rollback_in_create_with_record_invalid_exception
+ begin
+ Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
+ remove_method(:after_create_for_transaction)
+ def after_create_for_transaction
+ raise ActiveRecord::RecordInvalid.new(Author.new)
+ end
+ eoruby
+
+ new_topic = Topic.create(:title => "A new topic")
+ assert !new_topic.persisted?, "The topic should not be persisted"
+ assert_nil new_topic.id, "The topic should not have an ID"
+ ensure
+ remove_exception_raising_after_create_callback_to_topic
+ end
+ end
+
def test_nested_explicit_transactions
Topic.transaction do
Topic.transaction do