Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make sure AR::Base#clone handles attr changes made in after_initializ…

…e hooks. Closes #7191 [weyus]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7802 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3122d321fd5d05d549274487e5a52f3a73bc6f43 1 parent 4eaa8ba
@NZKoz NZKoz authored
View
6 activerecord/lib/active_record/base.rb
@@ -1751,9 +1751,9 @@ def destroy
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
4 activerecord/test/base_test.rb
@@ -1008,6 +1008,10 @@ def test_clone
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
8 activerecord/test/fixtures/topic.rb
@@ -28,4 +28,10 @@ def default_written_on
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
Please sign in to comment.
Something went wrong with that request. Please try again.