From c7f669af6f1d8e9053a586c97584702971e1906c Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 20 Jun 2017 17:35:13 -0700 Subject: Clear offset cache on CollectionProxy reset/reload The `@offsets` cache is used by FinderMethods to cache records found by find_nth. This cache is cleared in AR::Relation#reset, but not in CollectionProxy#reset or CollectionProxy#reload. Because of this, since #29098, calling #first/#find_nth/etc after calling #reload or #reset on an association could return a stale record. This is an issue both when the full association target is loaded and when the item is loaded in #find_nth. This commit solves the problem by clearing the `@offsets` cache in CollectionProxy#reset and CollectionProxy#reload. --- activerecord/lib/active_record/associations/collection_proxy.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 8cdb508c43..88b05bbcfa 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -1088,6 +1088,7 @@ module ActiveRecord # # => [#] def reload proxy_association.reload + @offsets = {} reset_scope end @@ -1110,6 +1111,7 @@ module ActiveRecord def reset proxy_association.reset proxy_association.reset_scope + @offsets = {} reset_scope end -- cgit v1.2.3