diff options
author | eileencodes <eileencodes@gmail.com> | 2017-02-28 08:06:42 -0500 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2017-02-28 08:48:37 -0500 |
commit | ca8c21df0fdbf1f03ba2f7fb16b39c3282dc1be0 (patch) | |
tree | 2e6c8741d2557795681a3e7521c891ac2e9cfb5e /activerecord/lib/active_record | |
parent | c971e7f151e76510196bb4ad21c1d7cbb1d0fe96 (diff) | |
download | rails-ca8c21df0fdbf1f03ba2f7fb16b39c3282dc1be0.tar.gz rails-ca8c21df0fdbf1f03ba2f7fb16b39c3282dc1be0.tar.bz2 rails-ca8c21df0fdbf1f03ba2f7fb16b39c3282dc1be0.zip |
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
topic.replies.clear
saved_replies.each do |reply|
user.update_replies_count!
end
end
```
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
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 55bf2e0ff0..d35fe9fe50 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -33,6 +33,10 @@ module ActiveRecord super klass, klass.arel_table, klass.predicate_builder end + def initialize_dup(other) # :nodoc: + @association = @association.deep_dup + end + def target @association.target end |