diff options
author | st0012 <stan001212@gmail.com> | 2019-04-24 19:55:16 +0800 |
---|---|---|
committer | st0012 <stan001212@gmail.com> | 2019-05-03 19:24:53 +0800 |
commit | 83ca4a18f726e7860fde469372888fe5a122dc21 (patch) | |
tree | c832224df481f50f4a7d5ffdbc0a1e7f94f5e07d | |
parent | 5575bd7b22e8e11ba8e2fdac4a92aab931d4f9f9 (diff) | |
download | rails-83ca4a18f726e7860fde469372888fe5a122dc21.tar.gz rails-83ca4a18f726e7860fde469372888fe5a122dc21.tar.bz2 rails-83ca4a18f726e7860fde469372888fe5a122dc21.zip |
Add a section to introduce `pluck`'s eager loading behavior
-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. |