Skip to content

Commit

Permalink
Add changelog entry and guide updates for pluck with multiple columns
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Jun 22, 2012
1 parent 6aae17e commit e5cd300
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,14 @@
## Rails 4.0.0 (unreleased) ##

* Allow ActiveRecord::Relation#pluck to accept multiple columns. Returns an
array of arrays containing the type casted values:

Person.pluck(:id, :name)
# SELECT people.id, people.name FROM people
# [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]

*Jeroen van Ingen & Carlos Antonio da Silva*

* Improve the derivation of HABTM join table name to take account of nesting.
It now takes the table names of the two models, sorts them lexically and
then joins them, stripping any common prefix from the second table name.
Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -140,6 +140,7 @@ def calculate(operation, column_name, options = {})
#
# Person.pluck(:id, :name)
# # SELECT people.id, people.name FROM people
# # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
#
# Person.uniq.pluck(:role)
# # SELECT DISTINCT role FROM people
Expand Down
22 changes: 16 additions & 6 deletions guides/source/active_record_querying.textile
Expand Up @@ -609,8 +609,8 @@ And this will give you a single +Order+ object for each date where there are ord
The SQL that would be executed would be something like this:

<sql>
SELECT date(created_at) as ordered_date, sum(price) as total_price
FROM orders
SELECT date(created_at) as ordered_date, sum(price) as total_price
FROM orders
GROUP BY date(created_at)
</sql>

Expand All @@ -627,9 +627,9 @@ Order.select("date(created_at) as ordered_date, sum(price) as total_price").grou
The SQL that would be executed would be something like this:

<sql>
SELECT date(created_at) as ordered_date, sum(price) as total_price
FROM orders
GROUP BY date(created_at)
SELECT date(created_at) as ordered_date, sum(price) as total_price
FROM orders
GROUP BY date(created_at)
HAVING sum(price) > 100
</sql>

Expand Down Expand Up @@ -1286,26 +1286,36 @@ Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")

h3. +pluck+

<tt>pluck</tt> can be used to query a single column from the underlying table of a model. It accepts a column name as argument and returns an array of values of the specified column with the corresponding data type.
<tt>pluck</tt> can be used to query a single or multiple columns from the underlying table of a model. It accepts a list of column names as argument and returns an array of values of the specified columns with the corresponding data type.

<ruby>
Client.where(:active => true).pluck(:id)
# SELECT id FROM clients WHERE active = 1
# => [1, 2, 3]

Client.uniq.pluck(:role)
# SELECT DISTINCT role FROM clients
# => ['admin', 'member', 'guest']

Client.pluck(:id, :name)
# SELECT clients.id, clients.name FROM clients
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
</ruby>

+pluck+ makes it possible to replace code like

<ruby>
Client.select(:id).map { |c| c.id }
# or
Client.select(:id).map { |c| [c.id, c.name] }
</ruby>

with

<ruby>
Client.pluck(:id)
# or
Client.pluck(:id, :name)
</ruby>

h3. +ids+
Expand Down

0 comments on commit e5cd300

Please sign in to comment.