Memoize association.named_scope calls

This commit is contained in:
Pratik Naik 2010-04-05 21:54:39 +01:00
parent 72f89b5d97
commit 1f7b4447a9
2 changed files with 12 additions and 0 deletions

@ -405,6 +405,9 @@ def method_missing(method, *args)
else
super
end
elsif @reflection.klass.scopes[method]
@_scopes ||= {}
@_scopes[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) }
else
with_scope(construct_scope) do
if block_given?

@ -413,6 +413,15 @@ def test_nested_named_scopes_queries_size
Topic.approved.by_lifo.replied.written_before(Time.now).all
end
end
def test_named_scopes_are_cached_on_associations
post = posts(:welcome)
assert_equal post.comments.containing_the_letter_e.object_id, post.comments.containing_the_letter_e.object_id
post.comments.containing_the_letter_e.all # force load
assert_no_queries { post.comments.containing_the_letter_e.all }
end
end
class DynamicScopeMatchTest < ActiveRecord::TestCase