From 54a2bf66019d2694ff53f666765faf5bca927c09 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 25 Feb 2011 15:38:59 -0800 Subject: removing limits and offsets from COUNT queries unless both are specified. [#6268 state:resolved] --- activerecord/lib/active_record/relation/calculations.rb | 14 +++++++++++++- activerecord/lib/active_record/test_case.rb | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index abc4c54109..c1842b1a96 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -204,7 +204,19 @@ module ActiveRecord relation.select_values = [select_value] - type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation) + query_builder = relation.arel + + if operation == "count" + limit = relation.limit_value + offset = relation.offset_value + + unless limit && offset + query_builder.limit = nil + query_builder.offset = nil + end + end + + type_cast_calculated_value(@klass.connection.select_value(query_builder.to_sql), column_for(column_name), operation) end def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index 4e711c4884..29efbbcb8c 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -26,6 +26,7 @@ module ActiveRecord def assert_sql(*patterns_to_match) $queries_executed = [] yield + $queries_executed ensure failed_patterns = [] patterns_to_match.each do |pattern| -- cgit v1.2.3