<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thoughtbot="https://thoughtbot.com/feeds/" xmlns:feedpress="https://feed.press/xmlns" xmlns:media="http://search.yahoo.com/mrss/" xmlns:podcast="https://podcastindex.org/namespace/1.0">
  <feedpress:locale>en</feedpress:locale>
  <link rel="hub" href="https://feedpress.superfeedr.com/"/>
  <title>Giant Robots Smashing Into Other Giant Robots</title>
  <subtitle>Written by thoughtbot, your expert partner for design and development.
</subtitle>
  <id>https://robots.thoughtbot.com/</id>
  <link href="https://thoughtbot.com/blog"/>
  <link href="https://feed.thoughtbot.com/" rel="self"/>
  <updated>2026-12-02T00:00:00+00:00</updated>
  <author>
    <name>thoughtbot</name>
  </author>
  <entry>
    <title>When the N+1 fix becomes the problem</title>
    <link rel="alternate" href="https://feed.thoughtbot.com/link/24077/17547101/when-the-n-1-fix-becomes-the-problem"/>
    <author>
      <name>Trésor Bireke</name>
    </author>
    <id>https://thoughtbot.com/blog/when-the-n-1-fix-becomes-the-problem</id>
    <published>2026-12-02T00:00:00+00:00</published>
    <updated>2026-12-01T20:05:05Z</updated>
    <content type="html"><![CDATA[<p>I fixed an ActionText N+1 by preloading rich text bodies. Later I deleted that preload, and the home page went from around 10 seconds to 360 ms.</p>

<p>Both changes were correct.</p>
<h2 id="the-first-fix-preload-everything">
  
    The first fix: Preload everything
  
</h2>

<p>Our list pages show a card per article, with a short excerpt on each card. The card partial had one line for that:</p>
<div class="highlight"><pre class="highlight erb"><code><span class="cp">&lt;%=</span> <span class="n">rich_text_preview</span><span class="p">(</span><span class="n">article</span><span class="p">.</span><span class="nf">body</span><span class="p">,</span> <span class="ss">length: </span><span class="mi">120</span><span class="p">)</span> <span class="cp">%&gt;</span>
</code></pre></div>
<p>The helper renders the body with <code>to_s</code>, strips the HTML with Nokogiri, and truncates the text to 120 characters.</p>

<p>Without a preload, every card loads its rich text row in its own query. In a list view, that is an N+1. So I added a scope that preloads the full chain:</p>
<div class="highlight"><pre class="highlight ruby"><code><span class="n">scope</span> <span class="ss">:with_full_body</span><span class="p">,</span> <span class="o">-&gt;</span> <span class="p">{</span>
  <span class="n">includes</span><span class="p">(</span><span class="ss">rich_text_body: </span><span class="p">{</span> <span class="ss">embeds_attachments: :blob</span> <span class="p">})</span>
<span class="p">}</span>
</code></pre></div>
<p>Rails generates the same scope for you as <code>with_rich_text_body_and_embeds</code>.</p>

<p>The rich text rows now loaded in one query, and I moved on.</p>
<h2 id="what-that-one-line-really-costs">
  
    What that one line really costs
  
</h2>

<p>The preload made one query efficient. It did nothing about the work itself.</p>

<p>That helper call still hides three costs, per card, per request:</p>

<ol>
<li>
<strong>Load it.</strong> The full rich text body and its attachment blobs come out of Postgres. Our production bodies average 275KB, and the largest is 7.1MB.</li>
<li>
<strong>Render it.</strong> Calling <code>to_s</code> on an ActionText body goes through ActionView. It resolves every embedded attachment, builds variant URLs, and renders a partial per image.</li>
<li>
<strong>Parse it.</strong> Nokogiri parses the rendered HTML, only to keep 120 characters of text.</li>
</ol>

<p>The second cost has a catch. Rails finds each embedded attachment through its signed ID, one blob lookup at a time. The preloaded embeds are not used for rendering, which is a <a href="https://github.com/rails/rails/issues/37249">known limitation</a>. So part of my preload was loading rows that nothing read.</p>

<p>The home page has 49 cards. It loaded about 13MB of HTML from the database and took around 10 seconds to produce 49 short strings.</p>
<h2 id="the-second-fix-stop-doing-the-work-at-read-time">
  
    The second fix: Stop doing the work at read time
  
</h2>

<p>An excerpt only changes when the body changes. So we can compute it once, on save, and store it.</p>

<p>Every model with a rich body got an <code>excerpt</code> column, filled by a <code>before_save</code> callback:</p>
<div class="highlight"><pre class="highlight ruby"><code><span class="k">module</span> <span class="nn">RichBody</span>
  <span class="kp">extend</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Concern</span>

  <span class="no">BLOCK_TAGS</span> <span class="o">=</span> <span class="s2">"p, div, br, li, h1, blockquote, pre"</span>

  <span class="n">included</span> <span class="k">do</span>
    <span class="n">has_rich_text</span> <span class="ss">:body</span>

    <span class="n">before_save</span> <span class="ss">:set_excerpt</span><span class="p">,</span> <span class="ss">if: </span><span class="o">-&gt;</span> <span class="p">{</span>
      <span class="n">association</span><span class="p">(</span><span class="ss">:rich_text_body</span><span class="p">).</span><span class="nf">loaded?</span> <span class="o">&amp;&amp;</span> <span class="n">rich_text_body</span><span class="o">&amp;</span><span class="p">.</span><span class="nf">changed?</span>
    <span class="p">}</span>
  <span class="k">end</span>

  <span class="kp">private</span>

  <span class="k">def</span> <span class="nf">set_excerpt</span>
    <span class="n">html</span> <span class="o">=</span> <span class="n">rich_text_body</span><span class="p">.</span><span class="nf">body</span><span class="o">&amp;</span><span class="p">.</span><span class="nf">to_html</span><span class="p">.</span><span class="nf">to_s</span>
    <span class="n">fragment</span> <span class="o">=</span> <span class="no">Nokogiri</span><span class="o">::</span><span class="no">HTML5</span><span class="p">.</span><span class="nf">fragment</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>

    <span class="c1"># Keep words apart: "&lt;h1&gt;Title&lt;/h1&gt;&lt;p&gt;First&lt;/p&gt;" =&gt; "Title First"</span>
    <span class="n">fragment</span><span class="p">.</span><span class="nf">css</span><span class="p">(</span><span class="no">BLOCK_TAGS</span><span class="p">).</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">node</span><span class="o">|</span>
      <span class="n">node</span><span class="p">.</span><span class="nf">add_next_sibling</span><span class="p">(</span><span class="no">Nokogiri</span><span class="o">::</span><span class="no">XML</span><span class="o">::</span><span class="no">Text</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">" "</span><span class="p">,</span> <span class="n">fragment</span><span class="p">.</span><span class="nf">document</span><span class="p">))</span>
    <span class="k">end</span>

    <span class="nb">self</span><span class="p">.</span><span class="nf">excerpt</span> <span class="o">=</span> <span class="n">fragment</span><span class="p">.</span><span class="nf">text</span><span class="p">.</span><span class="nf">squish</span><span class="p">.</span><span class="nf">first</span><span class="p">(</span><span class="mi">300</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>
</code></pre></div>
<p>The generator reads <code>to_html</code>, not <code>to_s</code>. <code>to_html</code> returns the content as it is stored, with attachments still as bare <code>&lt;action-text-attachment&gt;</code> tags. <code>to_s</code> is the one that renders them. This means even the write path skips attachment rendering.</p>

<p>The migration that adds the column also backfills it from the stored HTML, so old rows are not blank.</p>

<p>The view becomes:</p>
<div class="highlight"><pre class="highlight erb"><code><span class="cp">&lt;%=</span> <span class="n">truncate</span><span class="p">(</span><span class="n">article</span><span class="p">.</span><span class="nf">excerpt</span><span class="p">,</span> <span class="ss">length: </span><span class="mi">120</span><span class="p">)</span> <span class="cp">%&gt;</span>
</code></pre></div>
<p>Then I deleted every <code>with_full_body</code> call on the list queries. List pages no longer load rich text at all.</p>

<p>The home page now responds in 360 ms.</p>
<h2 id="two-details-worth-copying">
  
    Two details worth copying
  
</h2>
<h3 id="guard-the-callback-with-loaded">
  
    Guard the callback with loaded?
  
</h3>

<p>Without the <code>loaded?</code> check, every save of an article lazy-loads its rich text just to ask if it changed. Toggling <code>published</code> would pull a multi-megabyte body out of the database for nothing.</p>

<p>If the association was never loaded, it can’t have changed. Assigning <code>article.body</code> always loads it first, so the guard never misses a real edit.</p>
<h3 id="store-more-than-you-display">
  
    Store more than you display
  
</h3>

<p>The column holds 300 characters, and the card shows 120. The slack lets different views truncate to different lengths without touching the database again.</p>
<h2 id="considerations">
  
    Considerations
  
</h2>

<ul>
<li>The callback runs when you save the parent record. If you update the rich text record directly, the excerpt goes stale.</li>
<li>If you change how the excerpt is built, you need to backfill again.</li>
<li>Switching the helper to <code>to_plain_text</code> would not have saved us. It skips the ActionView render, but it still loads the body and resolves every attachment.</li>
<li>For a <code>show</code> page, or for small bodies with no attachments, the preload is fine. This only pays off when a value is read far more often than it is written.</li>
</ul>
<h2 id="conclusion">
  
    Conclusion
  
</h2>

<p>Excerpts are read thousands of times per edit. Any computation with that read/write ratio belongs at write time.</p>

<p>The N+1 fix and the real fix are the same lesson at two altitudes. Preloading made one query efficient, but nobody asked whether the data should be fetched at all. The next time you fix a slow query, ask one more question: does this page need that data in the first place?</p>

<aside class="related-articles"><h2>If you enjoyed this post, you might also like:</h2>
<ul>
<li><a href="https://thoughtbot.com/blog/debugging-why-your-specs-have-slowed-down">Debugging Why Your Specs Have Slowed Down</a></li>
<li><a href="https://thoughtbot.com/blog/unpopular-developer-2-down-with-big-testing">Unpopular Developer 2: Down With Big Testing</a></li>
<li><a href="https://thoughtbot.com/blog/when-to-create">When to Create?</a></li>
</ul></aside>
<img src="https://feed.thoughtbot.com/link/24077/17547101.gif" height="1" width="1"/>]]></content>
    <summary>I fixed an ActionText N+1 by preloading rich text bodies. Then I deleted that preload, and the home page went from 10 seconds to 360 ms.
Both changes were correct.</summary>
    <thoughtbot:auto_social_share>true</thoughtbot:auto_social_share>
  </entry>
  <entry>
    <title>NWRUG's (Manchester, UK) December 2026 meeting — Festive social</title>
    <link rel="alternate" href="https://feed.thoughtbot.com/link/24077/17541701/nwrug-s-manchester-uk-december-2026-meeting-festive-social"/>
    <author>
      <name>Rob Whittaker</name>
    </author>
    <id>https://thoughtbot.com/blog/nwrug-s-manchester-uk-december-2026-meeting-festive-social</id>
    <published>2026-11-27T00:00:00+00:00</published>
    <updated>2026-11-26T12:31:02Z</updated>
    <content type="html"><![CDATA[<p>The next North West Ruby User Group in Manchester, UK is our annual festive social on Thursday 17 December at <a href="https://www.thecrownandkettle.com">The Crown &amp; Kettle</a>. As the year winds down, we’re swapping the usual talks and code for cosy chats, good company, and a drink of whatever tickles your fancy, with both alcoholic and non-alcoholic options. Bring your Ruby questions. Christmas jumpers are optional but encouraged.</p>

<p>We’ll be at the pub, at 2 Oldham Road, Manchester, M4 5FE, from 7:30pm. The first round of drinks is kindly provided by NWRUG’s sponsor <a href="https://thoughtbot.com/">thoughtbot</a>.</p>

<p><a href="https://fatso.ma/fm7h5">Register here</a> if you would like to attend, so we can plan for numbers. Full details on this (and all NWRUG events) over on <a href="https://nwrug.org/events/festive-social-2026">NWRUG’s website</a>.</p>

<aside class="related-articles"><h2>If you enjoyed this post, you might also like:</h2>
<ul>
<li><a href="https://thoughtbot.com/blog/thanksgiving-already">Thanksgiving already?</a></li>
<li><a href="https://thoughtbot.com/blog/2-years-of-hackfests">2 Years Of Hackfests</a></li>
<li><a href="https://thoughtbot.com/blog/bring-your-laptop-get-started-with-ruby-at-tuesdays">Bring your laptop, get started with Ruby at Tuesday’s Boston.rb meetup</a></li>
</ul></aside>
<img src="https://feed.thoughtbot.com/link/24077/17541701.gif" height="1" width="1"/>]]></content>
    <summary>Swap talks and code for chats and drinks at NWRUG's annual festive social this December.</summary>
    <thoughtbot:auto_social_share>true</thoughtbot:auto_social_share>
  </entry>
</feed>
