<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: NanaYaw</title>
    <description>The latest articles on DEV Community by NanaYaw (@nanayaw).</description>
    <link>https://dev.to/nanayaw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2588672%2Fc10130d2-f04b-4b11-a505-dc50559a65ec.jpeg</url>
      <title>DEV Community: NanaYaw</title>
      <link>https://dev.to/nanayaw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nanayaw"/>
    <language>en</language>
    <item>
      <title>[Boost]</title>
      <dc:creator>NanaYaw</dc:creator>
      <pubDate>Fri, 20 Dec 2024 02:20:19 +0000</pubDate>
      <link>https://dev.to/nanayaw/-2gch</link>
      <guid>https://dev.to/nanayaw/-2gch</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/nanayaw" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2588672%2Fc10130d2-f04b-4b11-a505-dc50559a65ec.jpeg" alt="nanayaw"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/nanayaw/understanding-eagerload-vs-includes-in-rails-14e7" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Understanding eager_load vs includes in Rails&lt;/h2&gt;
      &lt;h3&gt;NanaYaw ・ Dec 19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>rails</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding eager_load vs includes in Rails</title>
      <dc:creator>NanaYaw</dc:creator>
      <pubDate>Thu, 19 Dec 2024 11:28:42 +0000</pubDate>
      <link>https://dev.to/nanayaw/understanding-eagerload-vs-includes-in-rails-14e7</link>
      <guid>https://dev.to/nanayaw/understanding-eagerload-vs-includes-in-rails-14e7</guid>
      <description>&lt;p&gt;When working with associations in Rails, both eager_load and includes help you avoid the N+1 query problem by preloading associated records. However, they behave slightly differently in how they execute SQL queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is includes?
&lt;/h2&gt;

&lt;p&gt;The Rails guides define includes as: &lt;em&gt;With&amp;nbsp;includes, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This means that it preloads associated records to optimize queries, and dynamically decides between them to avoid the N+1 query problem, but it decides the SQL strategy dynamically based on usage.&lt;/p&gt;

&lt;p&gt;Associated records are only loaded eagerly when accessed; if you do not access them, Rails will not load them eagerly.&lt;/p&gt;

&lt;p&gt;Rails uses two strategies when associated records are accessed:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It separates queries for the parents and associated records.&lt;/li&gt;
&lt;li&gt;It joins tables if conditions are applied to the associated records - similar to eager load&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;SQL query strategy:&lt;/strong&gt; &lt;br&gt;
Executes multiple queries (parent + associated records) by default, unless certain conditions require a LEFT OUTER JOIN.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3864xquyfe4ece0oqzrf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3864xquyfe4ece0oqzrf.png" alt="Image description" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The code above executes two queries, the parent and the associated record:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;SELECT * FROM posts&lt;br&gt;
SELECT * FROM comments WHERE post_id IN (…)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If conditions are applied, includes will generate a single query using LEFT OUTER JOIN, thereby reducing the number of queries to one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fveqo7rmk9w3fe3wphs0n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fveqo7rmk9w3fe3wphs0n.png" alt="Image description" width="800" height="268"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Queries executed:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;SELECT "posts".* &lt;br&gt;
FROM "posts" &lt;br&gt;
LEFT OUTER JOIN "comments" &lt;br&gt;
ON "comments"."post_id" = "posts"."id" &lt;br&gt;
WHERE "comments"."body" = 'example';&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What is eager_load?
&lt;/h2&gt;

&lt;p&gt;With eager_load, Active Record loads all specified associations using a LEF OUTER JOIN.&lt;br&gt;
This forces Rails to load associated records using a single query and Always generates a LEFT OUTER JOIN between parent and associated records.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshi4gbu7x5ce60sxt3iw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshi4gbu7x5ce60sxt3iw.png" alt="Image description" width="800" height="613"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Query executed:&lt;br&gt;
SELECT posts.*, comments.* &lt;br&gt;
FROM posts &lt;br&gt;
LEFT OUTER JOIN comments ON comments.post_id = posts.id&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Differences&lt;/strong&gt;&lt;br&gt;
Includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Separate queries by default or joins when necessary.&lt;/li&gt;
&lt;li&gt;It is efficient if associations aren’t accessed.&lt;/li&gt;
&lt;li&gt;It is efficient when you might not access associated records or need dynamic optimization.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Eager load:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It always uses a LEFT OUTER JOIN.&lt;/li&gt;
&lt;li&gt;It loads everything upfront, even if unnecessary.&lt;/li&gt;
&lt;li&gt;Very efficient when you need all associations in one query.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How to Choose?&lt;/strong&gt;&lt;br&gt;
Use includes for most use cases, especially when you don’t always access associated records, and go for eager_load when you want everything in a single query or need to apply conditions on associated records.&lt;/p&gt;

&lt;p&gt;That’s it! You now know when to use includes and eager_load to optimize your ActiveRecord queries effectively.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
