1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reset named scope cache whenever the @target is reset

This commit is contained in:
Pratik Naik 2010-04-07 01:16:45 +01:00
parent eb063538bd
commit 48634bf59a
2 changed files with 19 additions and 2 deletions

View file

@ -105,6 +105,7 @@ module ActiveRecord
def reset def reset
reset_target! reset_target!
reset_named_scopes_cache!
@loaded = false @loaded = false
end end
@ -162,6 +163,7 @@ module ActiveRecord
load_target load_target
delete(@target) delete(@target)
reset_target! reset_target!
reset_named_scopes_cache!
end end
# Calculate sum using SQL, not Enumerable # Calculate sum using SQL, not Enumerable
@ -250,6 +252,7 @@ module ActiveRecord
load_target load_target
destroy(@target) destroy(@target)
reset_target! reset_target!
reset_named_scopes_cache!
end end
def create(attrs = {}) def create(attrs = {})
@ -406,8 +409,8 @@ module ActiveRecord
super super
end end
elsif @reflection.klass.scopes[method] elsif @reflection.klass.scopes[method]
@_scopes ||= {} @_named_scopes_cache ||= {}
@_scopes[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) } @_named_scopes_cache[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) }
else else
with_scope(construct_scope) do with_scope(construct_scope) do
if block_given? if block_given?
@ -428,6 +431,10 @@ module ActiveRecord
@target = Array.new @target = Array.new
end end
def reset_named_scopes_cache!
@_named_scopes_cache = {}
end
def find_target def find_target
records = records =
if @reflection.options[:finder_sql] if @reflection.options[:finder_sql]

View file

@ -422,6 +422,16 @@ class NamedScopeTest < ActiveRecord::TestCase
post.comments.containing_the_letter_e.all # force load post.comments.containing_the_letter_e.all # force load
assert_no_queries { post.comments.containing_the_letter_e.all } assert_no_queries { post.comments.containing_the_letter_e.all }
end end
def test_named_scopes_are_reset_on_association_reload
post = posts(:welcome)
[:destroy_all, :reset, :delete_all].each do |method|
before = post.comments.containing_the_letter_e
post.comments.send(method)
assert before.object_id != post.comments.containing_the_letter_e.object_id, "AssociationCollection##{method} should reset the named scopes cache"
end
end
end end
class DynamicScopeMatchTest < ActiveRecord::TestCase class DynamicScopeMatchTest < ActiveRecord::TestCase