aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xactiverecord/lib/active_record/base.rb6
-rwxr-xr-xactiverecord/test/base_test.rb4
-rwxr-xr-xactiverecord/test/fixtures/topic.rb8
3 files changed, 14 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 8b562e2ffb..8d98796af1 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -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.
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 650d87f91f..5e20cbe127 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -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
diff --git a/activerecord/test/fixtures/topic.rb b/activerecord/test/fixtures/topic.rb
index dd71d6bf2b..b2541c8640 100755
--- a/activerecord/test/fixtures/topic.rb
+++ b/activerecord/test/fixtures/topic.rb
@@ -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 \ No newline at end of file