aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb15
-rw-r--r--activerecord/test/cases/calculations_test.rb6
2 files changed, 16 insertions, 5 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
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index f8bd11e3d0..041f8ffb7c 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -470,7 +470,11 @@ class CalculationsTest < ActiveRecord::TestCase
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))')
+
+ # 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.
+ assert_equal [50 + 53 + 55 + 60], Account.pluck('SUM(DISTINCT(credit_limit)) as credit_limit')
end
def test_pluck_expects_a_single_selection