diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0f544d1b5d11616bd82fbfc0d799ffd2ffba32d3..59d586c6f8e5c9af5c76bc67de8cb57782a71155 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Calling reset on a collection association should unload the assocation. + + Fixes #13777. + + *Kelsey Schlarman* + * Make enum fields work as expected with the `ActiveModel::Dirty` API. Before this change, using the dirty API would have surprising results: diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index a67b834a8027afbb3f1febb8ab59a1da3d1be952..08bc409816a2566e0634bc4fa9a4869e5980b378 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -1004,6 +1004,27 @@ def reload proxy_association.reload self end + + # Unloads the association + # + # class Person < ActiveRecord::Base + # has_many :pets + # end + # + # person.pets # fetches pets from the database + # # => [#] + # + # person.pets # uses the pets cache + # # => [#] + # + # person.pets.reset # clears the pets cache + # + # person.pets # fetches pets from the database + # # => [#] + def reset + proxy_association.reset + proxy_association.reset_scope + end end end end diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 48e6fc5cd4533374f4f63ef71bda11bb23b753bd..f663b5490c49d1249d559f7b847e06f6b57b9efa 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -255,6 +255,15 @@ def test_scoped_allows_conditions assert_equal man, man.interests.where("1=1").first.man end end + + def test_reset_unloads_target + david = authors(:david) + david.posts.reload + + assert david.posts.loaded? + david.posts.reset + assert !david.posts.loaded? + end end class OverridingAssociationsTest < ActiveRecord::TestCase