Skip to content

Commit

Permalink
mysql does not return alias names, so fall back
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Dec 29, 2012
1 parent 8a130ec commit 8faf2b3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -231,6 +231,8 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
# Postgresql doesn't like ORDER BY when there are no GROUP BY
relation = reorder(nil)

column_alias = column_name

if operation == "count" && (relation.limit_value || relation.offset_value)
# Shortcut when limit is zero.
return 0 if relation.limit_value == 0
Expand All @@ -241,16 +243,20 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:

select_value = operation_over_aggregate_column(column, operation, distinct)

column_name = select_value.alias
column_alias = select_value.alias
relation.select_values = [select_value]

query_builder = relation.arel
end

result = @klass.connection.select_all(query_builder, nil, relation.bind_values)
row = result.first
value = row && row.values.first
type_cast_calculated_value(value, result.column_types[column_name], operation)
row = result.first
value = row && row.values.first
column = result.column_types.fetch(column_alias) do
column_for(column_name)
end

type_cast_calculated_value(value, column, operation)
end

def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
Expand Down

0 comments on commit 8faf2b3

Please sign in to comment.