diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-03-27 15:02:48 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-03-27 15:02:48 -0300 |
commit | bdab7b15d12bdfec37b399cee9a889510a221d22 (patch) | |
tree | 95fe12c4d22e8225a31d19a54556a0b2b2f99a3e | |
parent | eddcdb0f1de6e7b1b503a6df8d60cd6a145ce080 (diff) | |
parent | 3a661794382c3b06cbb4d6dcc1859dd569cf8652 (diff) | |
download | rails-bdab7b15d12bdfec37b399cee9a889510a221d22.tar.gz rails-bdab7b15d12bdfec37b399cee9a889510a221d22.tar.bz2 rails-bdab7b15d12bdfec37b399cee9a889510a221d22.zip |
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
-rw-r--r-- | activerecord/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/models/movie.rb | 2 |
4 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 916d24aa03..b08f6ff38b 100644 --- a/activerecord/CHANGELOG.md +++ b/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. diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index ec3e8f281b..17f76b63b3 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -369,7 +369,7 @@ module ActiveRecord @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) diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 1664f1a096..e6ed85394b 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -5,6 +5,7 @@ require 'models/developer' 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 @@ class TransactionTest < ActiveRecord::TestCase @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? diff --git a/activerecord/test/models/movie.rb b/activerecord/test/models/movie.rb index c441be2bef..0302abad1e 100644 --- a/activerecord/test/models/movie.rb +++ b/activerecord/test/models/movie.rb @@ -1,3 +1,5 @@ class Movie < ActiveRecord::Base self.primary_key = "movieid" + + validates_presence_of :name end |