From c90f16da626a7edac45f7027b127ad874619455a Mon Sep 17 00:00:00 2001 From: Erich Menge Date: Tue, 15 May 2012 19:51:27 -0500 Subject: Fixes issue where SQL fragments prevented type casting based on column type. --- activerecord/lib/active_record/relation/calculations.rb | 11 +++-------- activerecord/test/cases/calculations_test.rb | 3 +++ 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index aa2f325f74..862009b1ba 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -147,17 +147,12 @@ module ActiveRecord end result = klass.connection.select_all(select(column_name).arel, nil, bind_values) - types = result.column_types.merge klass.column_types - column = types[key] + column = klass.column_types[key] || result.column_types.values.first result.map do |attributes| raise ArgumentError, "Pluck expects to select just one attribute: #{attributes.inspect}" unless attributes.one? - value = klass.initialize_attributes(attributes).first[1] - if column - column.type_cast value - else - value - end + value = klass.initialize_attributes(attributes).values.first + column ? column.type_cast(value) : value end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index c9a70bae77..f8bd11e3d0 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -468,6 +468,9 @@ class CalculationsTest < ActiveRecord::TestCase def test_pluck_with_selection_clause assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT credit_limit').sort + assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT accounts.credit_limit').sort + assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT(credit_limit)').sort + assert_equal [50 + 53 + 55 + 60], Account.pluck('SUM(DISTINCT(credit_limit))') end def test_pluck_expects_a_single_selection -- cgit v1.2.3