mirror of
https://github.com/github/rails.git
synced 2026-01-09 14:48:01 -05:00
Merge pull request #8209 from senny/backport_8176
backport #8176, `#pluck` can be used on a relation with `select` clause. Conflicts: activerecord/CHANGELOG.md
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -178,7 +178,9 @@ module ActiveRecord
|
||||
#
|
||||
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
|
||||
|
||||
@@ -487,4 +487,10 @@ class CalculationsTest < ActiveRecord::TestCase
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user