Browse files

Dupping a CollectionProxy should dup the load_target

In Rails 3.2 dupping a `CollectionProxy` would dup it's `load_target` as
well. That functionality has been broken since the release of Rails 4.0.
I hit this in an application upgrade and wondered why duplicating a
CollectionProxy and assigning it to a variable stopped working.

When calling `dup` on a `CollectionProxy` only the owner (ex.
topic) was getting duplicated and the `load_target` would remain in tact
with it's original object ID. Dupping the `load_target` is useful for performing
a logging operation after records have been destroyed in a method.

For example:

def transfer_operation
  saved_replies = topic.replies


  saved_replies.each do |reply|

This change adds a `initialize_dup` method that performs a `deep_dup` on
the `@associatiation` so that the `load_target` is dupped as well.

Fixes #17117
  • Loading branch information...
eileencodes committed Feb 28, 2017
1 parent f57ea18 commit 219402901fd076539ccab47996d51dd5e2ba54e4
@@ -36,6 +36,10 @@ def initialize(klass, association) #:nodoc:
merge! association.scope(nullify: false)
+ def initialize_dup(other) # :nodoc:
+ @association = @association.deep_dup
+ end
def target
@@ -2056,6 +2056,14 @@ def test_to_a_should_dup_target
assert_not_equal target.object_id, ary.object_id
+ def test_dup_should_dup_load_target
+ original = topics(:first).replies
+ dupped = topics(:first).replies.dup
+ assert_not_equal original.object_id, dupped.object_id
+ assert_not_equal original.load_target.object_id, dupped.load_target.object_id
+ end
def test_merging_with_custom_attribute_writer
bulb = => "red")
assert_equal "RED!", bulb.color

0 comments on commit 2194029

Please sign in to comment.