diff options
Diffstat (limited to 'activerecord/lib/active_record/relation')
3 files changed, 26 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index ccc14dddeb..3f154bd1cc 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -1,5 +1,3 @@ -require 'active_support/deprecation' - module ActiveRecord module Calculations # Count the records. @@ -231,6 +229,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,13 +241,20 @@ module ActiveRecord select_value = operation_over_aggregate_column(column, operation, distinct) + column_alias = select_value.alias relation.select_values = [select_value] query_builder = relation.arel end - result = @klass.connection.select_value(query_builder, nil, relation.bind_values) - type_cast_calculated_value(result, column_for(column_name), operation) + result = @klass.connection.select_all(query_builder, nil, relation.bind_values) + 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: @@ -265,7 +272,7 @@ module ActiveRecord column_alias_for(field) } group_columns = group_aliases.zip(group_fields).map { |aliaz,field| - [aliaz, column_for(field)] + [aliaz, field] } group = group_fields @@ -305,7 +312,10 @@ module ActiveRecord end Hash[calculated_data.map do |row| - key = group_columns.map { |aliaz, column| + key = group_columns.map { |aliaz, col_name| + column = calculated_data.column_types.fetch(aliaz) do + column_for(col_name) + end type_cast_calculated_value(row[aliaz], column) } key = key.first if key.size == 1 diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb index 431d083f21..615309964c 100644 --- a/activerecord/lib/active_record/relation/delegation.rb +++ b/activerecord/lib/active_record/relation/delegation.rb @@ -1,4 +1,3 @@ -require 'active_support/concern' require 'thread' require 'thread_safe' diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index d417e82548..de784f9f57 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -50,10 +50,7 @@ module ActiveRecord # Post.order('id asc').except(:order) # discards the order condition # Post.where('id > 10').order('id asc').except(:where) # discards the where condition but keeps the order def except(*skips) - result = Relation.new(klass, table, values.except(*skips)) - result.default_scoped = default_scoped - result.extend(*extending_values) if extending_values.any? - result + relation_with values.except(*skips) end # Removes any condition from the query other than the one(s) specified in +onlies+. @@ -61,11 +58,16 @@ module ActiveRecord # Post.order('id asc').only(:where) # discards the order condition # Post.order('id asc').only(:where, :order) # uses the specified order def only(*onlies) - result = Relation.new(klass, table, values.slice(*onlies)) - result.default_scoped = default_scoped - result.extend(*extending_values) if extending_values.any? - result + relation_with values.slice(*onlies) end + private + + def relation_with(values) # :nodoc: + result = Relation.new(klass, table, values) + result.default_scoped = default_scoped + result.extend(*extending_values) if extending_values.any? + result + end end end |