Skip to content
Browse files

Properly cache association_collection#scopes calls having arguments

  • Loading branch information...
1 parent 3a066e8 commit 517f709b51d1d2766d1a783d9f02fa7a6a41abc4 @lifo lifo committed Jun 4, 2010
View
3 activerecord/lib/active_record/associations/association_collection.rb
@@ -411,7 +411,8 @@ def method_missing(method, *args)
end
elsif @reflection.klass.scopes[method]
@_named_scopes_cache ||= {}
- @_named_scopes_cache[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) }
+ @_named_scopes_cache[method] ||= {}
+ @_named_scopes_cache[method][args] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) }
else
with_scope(construct_scope) do
if block_given?
View
13 activerecord/test/cases/named_scope_test.rb
@@ -428,6 +428,19 @@ def test_named_scopes_are_cached_on_associations
assert_no_queries { post.comments.containing_the_letter_e.all }
end
+ def test_named_scopes_with_arguments_are_cached_on_associations
+ post = posts(:welcome)
+
+ one = post.comments.limit_by(1).all
+ assert_equal 1, one.size
+
+ two = 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 }
+ end
+
def test_named_scopes_are_reset_on_association_reload
post = posts(:welcome)
View
1 activerecord/test/models/comment.rb
@@ -1,4 +1,5 @@
class Comment < ActiveRecord::Base
+ scope :limit_by, lambda {|l| limit(l) }
scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
scope :for_first_post, :conditions => { :post_id => 1 }
scope :for_first_author,

0 comments on commit 517f709

Please sign in to comment.
Something went wrong with that request. Please try again.