Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore state on create when ActiveRecord::RecordInvalid is raised #6073

Merged
merged 1 commit into from Aug 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* Fix AR#create to return an unsaved record when AR::RecordInvalid is
raised. Fixes #3217.

*Dave Yeu*

* Fixed table name prefix that is generated in engines for namespaced models
*Wojciech Wnętrzak*

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/transactions.rb
Expand Up @@ -327,7 +327,7 @@ def clear_transaction_record_state #:nodoc:
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
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/transactions_test.rb
Expand Up @@ -204,6 +204,23 @@ def test_callback_rollback_in_create
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
Expand Down