提交 43675f01 编写于 作者: K Kelsey Schlarman

Calling reset on a collection association should unload the assocation

Need to define #reset on CollectionProxy.
上级 a4ce0659
* 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:
......
......@@ -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
# # => [#<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
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册