aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-22 13:16:35 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-22 13:16:35 -0200
commit66aa5e252153ed2a00e7f1e8faa33f6eaa0bac8e (patch)
tree309353a1d64e84eca9259fd96bc10498a260c4f7 /activerecord
parent75cd7bc3c2fe1dca24dd1b03d61e9ff173b3d65f (diff)
parent43675f014c8d0913650823a5a3e92c8555a3caed (diff)
downloadrails-66aa5e252153ed2a00e7f1e8faa33f6eaa0bac8e.tar.gz
rails-66aa5e252153ed2a00e7f1e8faa33f6eaa0bac8e.tar.bz2
rails-66aa5e252153ed2a00e7f1e8faa33f6eaa0bac8e.zip
Merge pull request #13790 from kschlarman/collection_association_reset_fix
Calling reset on a collection association should unload the assocation
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb21
-rw-r--r--activerecord/test/cases/associations_test.rb9
3 files changed, 36 insertions, 0 deletions
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 @@ module ActiveRecord
proxy_association.reload
self
end
+
+ # Unloads the association
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets
+ # end
+ #
+ # person.pets # fetches pets from the database
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
+ #
+ # person.pets # uses the pets cache
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
+ #
+ # person.pets.reset # clears the pets cache
+ #
+ # person.pets # fetches pets from the database
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
+ 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 @@ class AssociationProxyTest < ActiveRecord::TestCase
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