Permalink
Browse files

`#pluck` can be used on a relation with `select` clause.

Closes #7551
  • Loading branch information...
1 parent d5ac280 commit 663d9ef67003d3bec44295d29f3c254951202926 @senny senny committed with senny Nov 11, 2012
View
9 activerecord/CHANGELOG.md
@@ -1,5 +1,14 @@
## Rails 4.0.0 (unreleased) ##
+* `#pluck` can be used on a relation with `select` clause
+ Fix #7551
+
+ Example:
+
+ Topic.select([:approved, :id]).order(:id).pluck(:id)
+
+ *Yves Senn*
+
* Do not create useless database transaction when building `has_one` association.
Example:
View
4 activerecord/lib/active_record/relation/calculations.rb
@@ -165,7 +165,9 @@ def pluck(*column_names)
if has_include?(column_names.first)
construct_relation_for_association_calculations.pluck(*column_names)
else
- result = klass.connection.select_all(select(column_names).arel, nil, bind_values)
+ relation = spawn
+ relation.select_values = column_names
+ result = klass.connection.select_all(relation.arel, nil, bind_values)
columns = result.columns.map do |key|
klass.column_types.fetch(key) {
result.column_types.fetch(key) {
View
6 activerecord/test/cases/calculations_test.rb
@@ -580,4 +580,10 @@ def test_pluck_with_reserved_words
assert_equal ["Over There"], Possession.pluck(:where)
end
+
+ def test_pluck_replaces_select_clause
+ taks_relation = Topic.select(:approved, :id).order(:id)
+ assert_equal [1,2,3,4], taks_relation.pluck(:id)
+ assert_equal [false, true, true, true], taks_relation.pluck(:approved)
+ end
end

0 comments on commit 663d9ef

Please sign in to comment.