From 11b25e0790522f2a702f4771f83c077bd21920c3 Mon Sep 17 00:00:00 2001 From: Petrik Date: Wed, 29 May 2024 16:36:24 +0200 Subject: [PATCH] Fix query result of eager_load example in the guides. [ci-skip] Calling `eager_load` will execute a single query with a join, not two. The original queries were probably the result of executing the code in the console, where `books = Book.eager_load(:author).limit(10) ` would execute the query. --- guides/source/active_record_querying.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index f7b6830d18..9e56388f91 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -1744,13 +1744,12 @@ books.each do |book| end ``` -The above code will execute just **2** queries, as opposed to the **11** queries from the original case: +The above code will execute just **1** query, as opposed to the **11** queries from the original case: ```sql -SELECT DISTINCT books.id FROM books LEFT OUTER JOIN authors ON authors.id = books.author_id LIMIT 10 -SELECT books.id AS t0_r0, books.last_name AS t0_r1, ... - FROM books LEFT OUTER JOIN authors ON authors.id = books.author_id - WHERE books.id IN (1,2,3,4,5,6,7,8,9,10) +SELECT "books"."id" AS t0_r0, "books"."title" AS t0_r1, ... FROM "books" + LEFT OUTER JOIN "authors" ON "authors"."id" = "books"."author_id" + LIMIT 10 ``` NOTE: The `eager_load` method uses an array, hash, or a nested hash of array/hash in the same way as the `includes` method to load any number of associations with a single `Model.find` call. Also, like the `includes` method, you can specify conditions for eager loaded associations.