1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #36077 from st0012/update-doc-for-pluck

Add a section to introduce pluck's eager loading behavior [ci skip]
This commit is contained in:
Prem Sichanugrist 2019-05-29 17:35:53 +09:00 committed by GitHub
commit 756333e684
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1821,6 +1821,21 @@ Client.limit(1).pluck(:name)
# => ["David"]
```
NOTE: You should also know that using `pluck` will trigger eager loading if the relation object contains include values, even if the eager loading is not necessary for the query. For example:
```ruby
# store association for reusing it
assoc = Company.includes(:account)
assoc.pluck(:id)
# SELECT "companies"."id" FROM "companies" LEFT OUTER JOIN "accounts" ON "accounts"."id" = "companies"."account_id"
```
One way to avoid this is to `unscope` the includes:
```ruby
assoc.unscope(:includes).pluck(:id)
```
### `ids`
`ids` can be used to pluck all the IDs for the relation using the table's primary key.