Permalink
Browse files

Merge pull request #14393 from chrisfinne/persisted_exception

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
	activerecord/test/models/movie.rb
  • Loading branch information...
1 parent ba70017 commit 93ed89df0a004e99441a87e5e27fdbda1ecfc0dc @rafaelfranca rafaelfranca committed Mar 27, 2014
@@ -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.
@@ -370,7 +370,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)
@@ -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
@@ -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?
@@ -2,4 +2,6 @@ class Movie < ActiveRecord::Base
def self.primary_key
"movieid"
end
+
+ validates_presence_of :name
end

0 comments on commit 93ed89d

Please sign in to comment.