Skip to content
This repository
Browse code

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

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

0 comments on commit 663d9ef

Please sign in to comment.
Something went wrong with that request. Please try again.