From e0eef111a31e1e442b60015b7c752cc1e2c21c57 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 9 Feb 2012 14:42:39 -0800 Subject: typecast columns based on the returned types --- activerecord/lib/active_record/relation/calculations.rb | 16 ++++++++++++++-- activerecord/test/cases/calculations_test.rb | 7 +------ 2 files changed, 15 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 50239f7cb2..63365e501b 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -177,11 +177,23 @@ module ActiveRecord # Person.where(:confirmed => true).limit(5).pluck(:id) # 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 - klass.connection.select_all(select(column_name).arel).map! do |attributes| - klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes)) + + result = klass.connection.select_all(select(column_name).arel) + types = result.column_types.merge klass.column_types + column = types[key] + + result.map do |attributes| + value = klass.initialize_attributes(attributes)[key] + if column + column.type_cast value + else + value + end end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index e1544b3b00..0391319a00 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -486,11 +486,6 @@ class CalculationsTest < ActiveRecord::TestCase def test_pluck_not_auto_table_name_prefix_if_column_joined Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) - # FIXME: PostgreSQL should works in the same way of the other adapters - if current_adapter?(:PostgreSQLAdapter) - assert_equal ["7"], Company.joins(:contracts).pluck(:developer_id) - else - assert_equal [7], Company.joins(:contracts).pluck(:developer_id) - end + assert_equal [7], Company.joins(:contracts).pluck(:developer_id) end end -- cgit v1.2.3