diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 51e8593c31098c56ea744c11b4f15b42d0067d32..0a5c42b17812bbfcce45520af78fabbc1fb09e89 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -1118,6 +1118,7 @@ def reset end def reset_scope # :nodoc: + @offsets = {} @scope = nil self end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 0cb7db1c2841261fbe31c28d80b4ce036703a9d4..6194786c10673eac14ab0654ee747b53a2a01685 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -741,6 +741,41 @@ def test_find_first_sanitized assert_equal client2, firm.clients.merge!(where: ["#{QUOTED_TYPE} = :type", { type: "Client" }], order: "id").first end + def test_find_first_after_reset_scope + firm = Firm.all.merge!(order: "id").first + collection = firm.clients + + original_object_id = collection.first.object_id + assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object" + + # It should return a different object, since the association has been reloaded + assert_not_equal original_object_id, firm.clients.first.object_id, "Expected #first to return a new object" + end + + def test_find_first_after_reset + firm = Firm.all.merge!(order: "id").first + collection = firm.clients + + original_object_id = collection.first.object_id + assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object" + collection.reset + + # It should return a different object, since the association has been reloaded + assert_not_equal original_object_id, collection.first.object_id, "Expected #first after #reload to return a new object" + end + + def test_find_first_after_reload + firm = Firm.all.merge!(order: "id").first + collection = firm.clients + + original_object_id = collection.first.object_id + assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object" + collection.reset + + # It should return a different object, since the association has been reloaded + assert_not_equal original_object_id, collection.first.object_id, "Expected #first after #reload to return a new object" + end + def test_find_all_with_include_and_conditions assert_nothing_raised do Developer.all.merge!(joins: :audit_logs, where: { "audit_logs.message" => nil, :name => "Smith" }).to_a