From 2a38fd58a91a300da5acfe917e82ac681dbd5b50 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 16 May 2012 14:07:10 -0700 Subject: MySQL returns "SUM(DISTINCT(credit_limit))" as the column name unless an alias is provided. Without the alias, the column cannot be found and properly typecast. --- activerecord/lib/active_record/relation/calculations.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 862009b1ba..aa9f7b71bf 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -140,19 +140,26 @@ module ActiveRecord # # => ['0', '27761', '173'] # def pluck(column_name) - key = column_name.to_s.split('.', 2).last - if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s) column_name = "#{table_name}.#{column_name}" end result = klass.connection.select_all(select(column_name).arel, nil, bind_values) - column = klass.column_types[key] || result.column_types.values.first + + key = column = nil + nullcast = 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 - column ? column.type_cast(value) : value + + key ||= attributes.keys.first + column ||= klass.column_types.fetch(key) { + result.column_types.fetch(key, nullcast) + } + + column.type_cast(value) end end -- cgit v1.2.3