Skip to content

Commit

Permalink
Ensure named_scope#empty? uses count query. [#262 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
ryanb authored and lifo committed May 28, 2008
1 parent 888a292 commit c2fbcba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/named_scope.rb
Expand Up @@ -102,7 +102,7 @@ class Scope
attr_reader :proxy_scope, :proxy_options attr_reader :proxy_scope, :proxy_options


[].methods.each do |m| [].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 delegate m, :to => :proxy_found
end end
end end
Expand Down Expand Up @@ -135,6 +135,10 @@ def last(*args)
end end
end end


def empty?
@found ? @found.empty? : count.zero?
end

protected protected
def proxy_found def proxy_found
@found || load_found @found || load_found
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -146,4 +146,12 @@ def test_first_and_last_find_options_should_use_query_when_results_are_loaded
end end
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 end

0 comments on commit c2fbcba

Please sign in to comment.