Skip to content

Commit

Permalink
typecast columns based on the returned types
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 9, 2012
1 parent e0cba3a commit e0eef11
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
16 changes: 14 additions & 2 deletions activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -177,11 +177,23 @@ def calculate(operation, column_name, options = {})
# Person.where(:confirmed => true).limit(5).pluck(:id) # Person.where(:confirmed => true).limit(5).pluck(:id)
# #
def pluck(column_name) 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) if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
column_name = "#{table_name}.#{column_name}" column_name = "#{table_name}.#{column_name}"
end 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
end end


Expand Down
7 changes: 1 addition & 6 deletions activerecord/test/cases/calculations_test.rb
Expand Up @@ -486,11 +486,6 @@ def test_pluck_auto_table_name_prefix


def test_pluck_not_auto_table_name_prefix_if_column_joined def test_pluck_not_auto_table_name_prefix_if_column_joined
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
# FIXME: PostgreSQL should works in the same way of the other adapters assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
if current_adapter?(:PostgreSQLAdapter)
assert_equal ["7"], Company.joins(:contracts).pluck(:developer_id)
else
assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
end
end end
end end

0 comments on commit e0eef11

Please sign in to comment.