diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-28 22:45:24 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-28 22:45:32 -0800 |
commit | 8faf2b3d49cadd28509447edb0452dd7bfecbe5c (patch) | |
tree | 47dc7353427323b97c69b053fc1e0820eec84610 /activerecord/lib/active_record/relation | |
parent | 8a130ece7e265f1e94abef8d64d5c6880797e831 (diff) | |
download | rails-8faf2b3d49cadd28509447edb0452dd7bfecbe5c.tar.gz rails-8faf2b3d49cadd28509447edb0452dd7bfecbe5c.tar.bz2 rails-8faf2b3d49cadd28509447edb0452dd7bfecbe5c.zip |
mysql does not return alias names, so fall back
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index e27c64e3c9..70d07fdd36 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -231,6 +231,8 @@ module ActiveRecord # 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 @@ -241,16 +243,20 @@ module ActiveRecord 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: |