diff options
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index aa9f7b71bf..31d99f0192 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -146,19 +146,18 @@ module ActiveRecord result = klass.connection.select_all(select(column_name).arel, nil, bind_values) - key = column = nil - nullcast = Class.new { def type_cast(v); v; end }.new + key = result.columns.first + column = klass.column_types.fetch(key) { + result.column_types.fetch(key) { + Class.new { def type_cast(v); v; end }.new + } + } result.map do |attributes| raise ArgumentError, "Pluck expects to select just one attribute: #{attributes.inspect}" unless attributes.one? value = klass.initialize_attributes(attributes).values.first - key ||= attributes.keys.first - column ||= klass.column_types.fetch(key) { - result.column_types.fetch(key, nullcast) - } - column.type_cast(value) end end |