Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Document a difference between pluck and select [ci skip]

Explain that `pluck` differs from `select` in that it does not construct `ActiveRecord` objects and thus model-level overrides are unavailable.
  • Loading branch information...
commit 956a27ccd6aa119a5a64aa5714fc629c9d3317eb 1 parent beb5ea8
@egilburg egilburg authored
Showing with 33 additions and 2 deletions.
  1. +33 −2 guides/source/
35 guides/source/
@@ -1466,7 +1466,7 @@ Client.pluck(:id, :name)
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
-`pluck` makes it possible to replace code like
+`pluck` makes it possible to replace code like:
```ruby { |c| }
@@ -1476,7 +1476,7 @@, :name).map { |c| [,] }
@@ -1484,6 +1484,37 @@ Client.pluck(:id)
Client.pluck(:id, :name)
+Unlike `select`, `pluck` directly converts a database result into a Ruby `Array`,
+without constructing `ActiveRecord` objects. This can mean better performance for
+a large or often-running query. However, any model method overrides will
+not be available. For example:
+class Client < ActiveRecord::Base
+ def name
+ "I am #{super}"
+ end
+ &:name
+# => ["I am David", "I am Jeremy", "I am Jose"]
+# => ["David", "Jeremy", "Jose"]
+Furthermore, unlike `select` and other `Relation` scopes, `pluck` triggers an immediate
+query, and thus cannot be chained with any further scopes, although it can work with
+scopes already constructed earlier:
+# => NoMethodError: undefined method `limit' for #<Array:0x007ff34d3ad6d8>
+# => ["David"]
### `ids`
`ids` can be used to pluck all the IDs for the relation using the table's primary key.
Please sign in to comment.
Something went wrong with that request. Please try again.