Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

AR .persisted? throws SystemStackError for an unsaved model with a

custom primary_key that didn't save due to validation error
  • Loading branch information...
commit 3a661794382c3b06cbb4d6dcc1859dd569cf8652 1 parent eddcdb0
@chrisfinne chrisfinne authored rafaelfranca committed
View
7 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*
+
* Introduce `validate` as an alias for `valid?`.
This is more intuitive when you want to run validations but don't care about the return value.
View
2  activerecord/lib/active_record/transactions.rb
@@ -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)
View
6 activerecord/test/cases/transactions_test.rb
@@ -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?
View
2  activerecord/test/models/movie.rb
@@ -1,3 +1,5 @@
class Movie < ActiveRecord::Base
self.primary_key = "movieid"
+
+ validates_presence_of :name
end
Please sign in to comment.
Something went wrong with that request. Please try again.