From d6e4c06a8a995a572237ebca96fa1d320e2418b3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 16 May 2012 14:16:57 -0700 Subject: assuming there is only one column, we can simplify the type cast loop --- activerecord/lib/active_record/relation/calculations.rb | 13 ++++++------- 1 file 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 -- cgit v1.2.3