Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure named_scope#empty? uses count query. [#262 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit c2fbcba16d2349285368c86b849db946dc0e7d57 1 parent 888a292
@ryanb ryanb authored lifo committed
View
6 activerecord/lib/active_record/named_scope.rb
@@ -102,7 +102,7 @@ class Scope
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 @@ def last(*args)
end
end
+ def empty?
+ @found ? @found.empty? : count.zero?
+ end
+
protected
def proxy_found
@found || load_found
View
8 activerecord/test/cases/named_scope_test.rb
@@ -146,4 +146,12 @@ def test_first_and_last_find_options_should_use_query_when_results_are_loaded
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
Please sign in to comment.
Something went wrong with that request. Please try again.