Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport #8176, #pluck can be used on a relation with select clause. #8209

Merged
merged 1 commit into from Nov 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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*

* Do not create useless database transaction when building `has_one` association. [Backport #8154]

Example:
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/relation/calculations.rb
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
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