diff options
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 |