Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

AssociationCollection#to_ary should definitely dup the target! Also c…

…hanged #replace which was previously incorrect, but the test passed due to the fact that to_a was not duping.
  • Loading branch information...
commit 88df88095c82cde53501abe2a44f6c1f66c272b4 1 parent 1da1ac1
@jonleighton jonleighton authored
View
6 activerecord/lib/active_record/associations/association_collection.rb
@@ -45,7 +45,7 @@ def last(*args)
end
def to_ary
- load_target
+ load_target.dup
end
alias_method :to_a, :to_ary
@@ -289,13 +289,13 @@ def uniq(collection = self)
# This will perform a diff and delete/add only records that have changed.
def replace(other_array)
other_array.each { |val| raise_on_type_mismatch(val) }
-
- load_target
+ original_target = load_target.dup
transaction do
delete(@target - other_array)
unless concat(other_array - @target)
+ @target = original_target
raise RecordNotSaved, "Failed to replace #{@reflection.name} because one or more of the "
"new records could not be saved."
end
View
7 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1349,4 +1349,11 @@ def test_load_target_respects_protected_attributes
assert_equal reply.id, first.id
assert_equal true, first.approved?
end
+
+ def test_to_a_should_dup_target
+ ary = topics(:first).replies.to_a
+ target = topics(:first).replies.target
+
+ assert_not_equal target.object_id, ary.object_id
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.