Make sure AR::Base#clone handles attr changes made in after_initialize hooks. Closes #7191 [weyus]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7802 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Michael Koziarski
2007-10-08 06:21:38 +00:00
parent 4eaa8ba5be
commit 3122d321fd
3 changed files with 14 additions and 4 deletions

View File

@@ -1751,9 +1751,9 @@ module ActiveRecord #:nodoc:
def clone
attrs = self.attributes_before_type_cast
attrs.delete(self.class.primary_key)
self.class.new do |record|
record.send :instance_variable_set, '@attributes', attrs
end
record = self.class.new
record.send :instance_variable_set, '@attributes', attrs
record
end
# Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records.

View File

@@ -1008,6 +1008,10 @@ class BasicsTest < Test::Unit::TestCase
cloned_topic.title["a"] = "c"
assert_equal "b", topic.title["a"]
#test if attributes set as part of after_initialize are cloned correctly
assert_equal topic.author_email_address, cloned_topic.author_email_address
# test if saved clone object differs from original
cloned_topic.save
assert !cloned_topic.new_record?
assert cloned_topic.id != topic.id

View File

@@ -28,4 +28,10 @@ class Topic < ActiveRecord::Base
def destroy_children
self.class.delete_all "parent_id = #{id}"
end
end
def after_initialize
if self.new_record?
self.author_email_address = 'test@test.com'
end
end
end