From 43675f014c8d0913650823a5a3e92c8555a3caed Mon Sep 17 00:00:00 2001 From: Kelsey Schlarman Date: Tue, 21 Jan 2014 18:19:51 -0800 Subject: [PATCH] Calling reset on a collection association should unload the assocation Need to define #reset on CollectionProxy. --- activerecord/CHANGELOG.md | 6 ++++++ .../associations/collection_proxy.rb | 21 +++++++++++++++++++ activerecord/test/cases/associations_test.rb | 9 ++++++++ 3 files changed, 36 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0f544d1b5d..59d586c6f8 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 a67b834a80..08bc409816 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 48e6fc5cd4..f663b5490c 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 -- GitLab