diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-04-05 21:54:39 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-04-05 21:55:24 +0100 |
commit | 1f7b4447a9030ccec542ecaa1e5e11e0d4622a84 (patch) | |
tree | 8286cf66e11fee73ad22352e803d586e3ab44df6 /activerecord | |
parent | 72f89b5d971b48a133c4c0af56fbeda35d738dae (diff) | |
download | rails-1f7b4447a9030ccec542ecaa1e5e11e0d4622a84.tar.gz rails-1f7b4447a9030ccec542ecaa1e5e11e0d4622a84.tar.bz2 rails-1f7b4447a9030ccec542ecaa1e5e11e0d4622a84.zip |
Memoize association.named_scope calls
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 9 |
2 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 6eda70d0ce..5ecdf1ac8d 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -405,6 +405,9 @@ module ActiveRecord 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? diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 2396ca10b0..3d5ebb6cb8 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -413,6 +413,15 @@ class NamedScopeTest < ActiveRecord::TestCase 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 |