Skip to content

Commit

Permalink
Merge pull request #8209 from senny/backport_8176
Browse files Browse the repository at this point in the history
backport #8176, `#pluck` can be used on a relation with `select` clause.

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Nov 13, 2012
2 parents e4e2bcc + d3006f3 commit 7240202
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## Rails 3.2.10 (unreleased)

* `#pluck` can be used on a relation with `select` clause. [Backport #8176]
Fix #7551

Example:

Topic.select([:approved, :id]).order(:id).pluck(:id)

*Yves Senn*

* Use `nil?` instead of `blank?` to check whether dynamic finder with a bang
should raise RecordNotFound.
Fixes #7238.
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def calculate(operation, column_name, options = {})
#
def pluck(column_name)
column_name = column_name.to_s
klass.connection.select_all(select(column_name).arel).map! do |attributes|
relation = clone
relation.select_values = [column_name]
klass.connection.select_all(relation.arel).map! do |attributes|
klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
end
end
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,10 @@ def test_pluck_with_serialization
def test_pluck_with_qualified_column_name
assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
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 7240202

Please sign in to comment.