Browse files

Honor distinct option when used with count operation after group clau…

…se [#5721 state:resolved]
  • Loading branch information...
1 parent 31358d8 commit 1c9022de212c190362d40b98624dcf71d20ca073 @marklazz marklazz committed with tenderlove Oct 1, 2010
View
12 activerecord/lib/active_record/relation/calculations.rb
@@ -177,7 +177,7 @@ def perform_calculation(operation, column_name, options = {})
distinct = options[:distinct] || distinct
if @group_values.any?
- execute_grouped_calculation(operation, column_name)
+ execute_grouped_calculation(operation, column_name, distinct)
else
execute_simple_calculation(operation, column_name, distinct)
end
@@ -191,19 +191,23 @@ def aggregate_column(column_name)
end
end
+ def operation_over_aggregate_column(column, operation, distinct)
+ operation == 'count' ? column.count(distinct) : column.send(operation)
+ end
+
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
column = aggregate_column(column_name)
# Postgresql doesn't like ORDER BY when there are no GROUP BY
relation = except(:order)
- select_value = operation == 'count' ? column.count(distinct) : column.send(operation)
+ select_value = operation_over_aggregate_column(column, operation, distinct)
relation.select_values = [select_value]
type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation)
end
- def execute_grouped_calculation(operation, column_name) #:nodoc:
+ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
group_attr = @group_values.first
association = @klass.reflect_on_association(group_attr.to_sym)
associated = association && association.macro == :belongs_to # only count belongs_to associations
@@ -221,7 +225,7 @@ def execute_grouped_calculation(operation, column_name) #:nodoc:
relation = except(:group).group(group)
relation.select_values = [
- aggregate_column(column_name).send(operation).as(aggregate_alias),
+ operation_over_aggregate_column(aggregate_column(column_name), operation, distinct).as(aggregate_alias),
"#{group_field} AS #{group_alias}"
]
View
9 activerecord/test/cases/calculations_test.rb
@@ -346,4 +346,13 @@ def test_from_option_with_specified_index
def test_from_option_with_table_different_than_class
assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
end
+
+ def test_distinct_is_honored_when_used_with_count_operation_after_group
+ # Count the number of authors for approved topics
+ approved_topics_count = Topic.group(:approved).count(:author_name)[true]
+ assert_equal approved_topics_count, 3
+ # Count the number of distinct authors for approved Topics
+ distinct_authors_for_approved_count = Topic.group(:approved).count(:author_name, :distinct => true)[true]
+ assert_equal distinct_authors_for_approved_count, 2
+ end
end

0 comments on commit 1c9022d

Please sign in to comment.