提交 3a661794 编写于 作者: C chrisfinne 提交者: Rafael Mendonça França

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

custom primary_key that didn't save due to validation error
上级 eddcdb0f
* 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.
......
......@@ -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)
......
......@@ -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?
......
class Movie < ActiveRecord::Base
self.primary_key = "movieid"
validates_presence_of :name
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册