提交 b6e15f9a 编写于 作者: S Sean Griffin

Don't error when `dup`ing a record with no PK

上级 5c87b5c5
......@@ -52,6 +52,12 @@ def initialize_clone(_)
super
end
def reset(key)
if include?(key)
write_from_database(key, nil)
end
end
protected
attr_reader :attributes
......
......@@ -315,9 +315,8 @@ def init_with(coder)
##
def initialize_dup(other) # :nodoc:
pk = self.class.primary_key
@attributes = @attributes.dup
@attributes.write_from_database(pk, nil)
@attributes.reset(self.class.primary_key)
run_callbacks(:initialize) unless _initialize_callbacks.empty?
......
......@@ -141,5 +141,17 @@ def test_dup_with_default_scope
ensure
Topic.default_scopes = prev_default_scopes
end
def test_dup_without_primary_key
klass = Class.new(ActiveRecord::Base) do
self.table_name = 'parrots_pirates'
end
record = klass.create!
assert_nothing_raised do
record.dup
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册