Permalink
Browse files

Merge pull request #6336 from erichmenge/patch-pluck-fragment

Fixes issue where SQL fragments prevented type casting based on column
  • Loading branch information...
2 parents e296269 + c90f16d commit aa964204b867b3e69c1e68fbaec101ce9cbaa0ef @carlosantoniodasilva carlosantoniodasilva committed May 16, 2012
@@ -147,17 +147,12 @@ def pluck(column_name)
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
@@ -468,6 +468,9 @@ def test_pluck_not_auto_table_name_prefix_if_column_joined
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

0 comments on commit aa96420

Please sign in to comment.