diff options
author | Ryan Bates <ryan@railscasts.com> | 2008-05-27 07:31:53 -0700 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-05-28 11:02:07 +0100 |
commit | c2fbcba16d2349285368c86b849db946dc0e7d57 (patch) | |
tree | 3cf328f85d012018e028c1abf42ece6b47d830c8 | |
parent | 888a2927b65889465ce7a1a71e87d37640a2b41b (diff) | |
download | rails-c2fbcba16d2349285368c86b849db946dc0e7d57.tar.gz rails-c2fbcba16d2349285368c86b849db946dc0e7d57.tar.bz2 rails-c2fbcba16d2349285368c86b849db946dc0e7d57.zip |
Ensure named_scope#empty? uses count query. [#262 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 8 |
2 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 69b7da7700..b0c8a8b815 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -102,7 +102,7 @@ module ActiveRecord attr_reader :proxy_scope, :proxy_options [].methods.each do |m| - unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last)/ + unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last|empty?)/ delegate m, :to => :proxy_found end end @@ -135,6 +135,10 @@ module ActiveRecord end end + def empty? + @found ? @found.empty? : count.zero? + end + protected def proxy_found @found || load_found diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 8f2fc53d67..d890cf7936 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -146,4 +146,12 @@ class NamedScopeTest < ActiveRecord::TestCase end end + def test_empty_should_not_load_results + topics = Topic.base + assert_queries(2) do + topics.empty? # use count query + topics.collect # force load + topics.empty? # use loaded (no query) + end + end end |