Skip to content

Commit

Permalink
Merge pull request #14393 from chrisfinne/persisted_exception
Browse files Browse the repository at this point in the history
AR .persisted? throws SystemStackError for an unsaved model with a
custom primary_key that didn't save due to validation error

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Mar 27, 2014
1 parent 9ec117d commit d6c704f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
* Fixed error where .persisted? throws SystemStackError for an unsaved model with a
custom primary key that didn't save due to validation error.

Fixes #14393.

*Chris Finne*

* `rake db:structure:dump` only dumps schema information if the schema
migration table exists.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/transactions.rb
Expand Up @@ -369,7 +369,7 @@ def restore_transaction_record_state(force = false) #:nodoc:
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
if restore_state.has_key?(:id)
self.id = restore_state[:id]
write_attribute(self.class.primary_key, restore_state[:id])
else
@attributes.delete(self.class.primary_key)
@attributes_cache.delete(self.class.primary_key)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/transactions_test.rb
Expand Up @@ -5,6 +5,7 @@
require 'models/book'
require 'models/author'
require 'models/post'
require 'models/movie'

class TransactionTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
Expand All @@ -14,6 +15,11 @@ def setup
@first, @second = Topic.find(1, 2).sort_by { |t| t.id }
end

def test_persisted_in_a_model_with_custom_primary_key_after_failed_save
movie = Movie.create
assert !movie.persisted?
end

def test_raise_after_destroy
assert_not @first.frozen?

Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/movie.rb
@@ -1,3 +1,5 @@
class Movie < ActiveRecord::Base
self.primary_key = "movieid"

validates_presence_of :name
end

0 comments on commit d6c704f

Please sign in to comment.