diff options
author | Prem Sichanugrist <s@sikac.hu> | 2019-05-29 17:35:53 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-29 17:35:53 +0900 |
commit | 756333e684e8b4fa6e93d56664f2147e201cb1e7 (patch) | |
tree | 4b3b2e8a1a14c5caa8c83a8cf8291aaec5fd010c /guides/source | |
parent | 784664f85b684ec3749c36f6f57a355631920cab (diff) | |
parent | 83ca4a18f726e7860fde469372888fe5a122dc21 (diff) | |
download | rails-756333e684e8b4fa6e93d56664f2147e201cb1e7.tar.gz rails-756333e684e8b4fa6e93d56664f2147e201cb1e7.tar.bz2 rails-756333e684e8b4fa6e93d56664f2147e201cb1e7.zip |
Merge pull request #36077 from st0012/update-doc-for-pluck
Add a section to introduce pluck's eager loading behavior [ci skip]
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_record_querying.md | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index e40f16e62d..5fb030fad4 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -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. |