Skip to content

Commit

Permalink
Removing the scope-caching which happens on association proxies, beca…
Browse files Browse the repository at this point in the history
…use the query is already cached by the query cacher. For formalised proof see http://www.youtube.com/watch?v=wDefXLb-FDs
  • Loading branch information
jonleighton authored and tenderlove committed Apr 13, 2011
1 parent a616e7a commit fc9a04b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
Expand Up @@ -21,14 +21,7 @@ class CollectionAssociation < Association #:nodoc:
attr_reader :proxy

def initialize(owner, reflection)
# When scopes are created via method_missing on the proxy, they are stored so that
# any records fetched from the database are kept around for future use.
@scopes_cache = Hash.new do |hash, method|
hash[method] = { }
end

super

@proxy = CollectionProxy.new(self)
end

Expand Down Expand Up @@ -74,7 +67,6 @@ def ids_writer(ids)
def reset
@loaded = false
@target = []
@scopes_cache.clear
end

def select(select = nil)
Expand Down Expand Up @@ -327,10 +319,6 @@ def include?(record)
end
end

def cached_scope(method, args)
@scopes_cache[method][args] ||= scoped.readonly(nil).send(method, *args)
end

def load_target
if find_target?
targets = []
Expand Down
Expand Up @@ -82,8 +82,6 @@ def method_missing(method, *args, &block)
end
end

elsif @association.klass.scopes[method]
@association.cached_scope(method, args)
else
scoped.readonly(nil).send(method, *args, &block)
end
Expand Down
25 changes: 15 additions & 10 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -440,26 +440,31 @@ def test_nested_scopes_queries_size
end
end

# Note: these next two are kinda odd because they are essentially just testing that the
# query cache works as it should, but they are here for legacy reasons as they was previously
# a separate cache on association proxies, and these show that that is not necessary.
def test_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 }
Post.cache do
assert_queries(1) { post.comments.containing_the_letter_e.all }
assert_no_queries { post.comments.containing_the_letter_e.all }
end
end

def test_scopes_with_arguments_are_cached_on_associations
post = posts(:welcome)

one = post.comments.limit_by(1).all
assert_equal 1, one.size
Post.cache do
one = assert_queries(1) { post.comments.limit_by(1).all }
assert_equal 1, one.size

two = post.comments.limit_by(2).all
assert_equal 2, two.size
two = assert_queries(1) { post.comments.limit_by(2).all }
assert_equal 2, two.size

assert_no_queries { post.comments.limit_by(1).all }
assert_no_queries { post.comments.limit_by(2).all }
assert_no_queries { post.comments.limit_by(1).all }
assert_no_queries { post.comments.limit_by(2).all }
end
end

def test_scopes_are_reset_on_association_reload
Expand Down

0 comments on commit fc9a04b

Please sign in to comment.