Fixed pluck to be working with selects.

See #9777 for details.

Previously pluck is not returning what we wanted to
Added a test also to make sure it's working fine.
 
This will also fix the build for 1.8.7 as we 
were doing some sort on hash.

Thanks @pixeltrix for helping me out.

Thanks @linduxed for pairing with me.
This commit is contained in:
Arun Agrawal
2013-05-10 15:41:51 +02:00
parent 6ab3c73062
commit de5b89ddc9
2 changed files with 7 additions and 3 deletions

View File

@@ -181,8 +181,11 @@ module ActiveRecord
column_name = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
end
result = klass.connection.exec_query(select(column_name).to_sql)
last_column = result.columns.last
klass.connection.select_all(select(column_name).arel).map! do |attributes|
klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
klass.type_cast_attribute(last_column, klass.initialize_attributes(attributes))
end
end

View File

@@ -494,8 +494,9 @@ class CalculationsTest < ActiveRecord::TestCase
end
def test_pluck_does_not_replace_select_clause
taks_relation = Topic.select("approved, id, id AS foo_id").order(:foo_id)
assert_equal [false, true, true, true], taks_relation.pluck(:approved)
taks_relation = Topic.select("approved, id, id AS foo_id").order('foo_id DESC')
assert_equal [4,3,2,1], taks_relation.pluck(:id)
assert_equal [true, true, true, false], taks_relation.pluck(:approved)
end
def test_pluck_auto_table_name_prefix