Permalink
Browse files

typecast columns based on the returned types

  • Loading branch information...
1 parent e0cba3a commit e0eef111a31e1e442b60015b7c752cc1e2c21c57 @tenderlove tenderlove committed Feb 9, 2012
@@ -177,11 +177,23 @@ def calculate(operation, column_name, options = {})
# 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
@@ -486,11 +486,6 @@ def test_pluck_auto_table_name_prefix
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

0 comments on commit e0eef11

Please sign in to comment.