diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-05-16 14:07:10 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-05-16 14:07:10 -0700 |
commit | 2a38fd58a91a300da5acfe917e82ac681dbd5b50 (patch) | |
tree | b1b66d504e959ef35e61836e054e1bd5ec05fd34 /activerecord/lib | |
parent | bf0212b92ab7ebf1f5c6971a1d8ae80e7c18dfb5 (diff) | |
download | rails-2a38fd58a91a300da5acfe917e82ac681dbd5b50.tar.gz rails-2a38fd58a91a300da5acfe917e82ac681dbd5b50.tar.bz2 rails-2a38fd58a91a300da5acfe917e82ac681dbd5b50.zip |
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 15 |
1 files changed, 11 insertions, 4 deletions
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 |