<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Activerecord on MoskitoHero</title><link>/tags/activerecord/</link><description>Recent content in Activerecord on MoskitoHero</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 06 May 2025 00:00:00 +0000</lastBuildDate><atom:link href="/tags/activerecord/index.xml" rel="self" type="application/rss+xml"/><item><title>An introduction to Barley, the fast model serializer</title><link>/post/2025-05-06-an-introduction-to-barley-the-fast-model-serializer/</link><pubDate>Tue, 06 May 2025 00:00:00 +0000</pubDate><guid>/post/2025-05-06-an-introduction-to-barley-the-fast-model-serializer/</guid><description>How I built and open-sourced Barley, a fast and simple model serializer gem for Rails, born from real-world performance needs and benchmarked against the best in the Ruby ecosystem.</description><content:encoded><![CDATA[<div class="sect1">
<h2 id="the-story"><a class="anchor" href="#the-story"></a>The story</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The company I used to work for had an ageing API in v1 that was very poorly designed and slow in its implementation. It served 4 frontend platforms (a website, two mobile apps and an administration tool). Being in a startup environment, our team had to move fast, but that was at the expense of performance and maintainability. This was the situation when I arrived there.
Returning JSON objects was very cumbersome. It used a custom method, available in each model that built a nested Hash, based on the attributes you gave it. That turned out to be a mess with poor performance and concerning security implications. It was difficult to maintain, with odd variable naming and behavior, so we set out to get rid of it somehow and moved to a kind of serializer DSL that was gradually introduced to replace the old mess.
Later, we decided to tighten up our API with a new version to get rid of the legacy. A few requirements quickly emerged, among which:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>API V2 should be easy to maintain, with clear naming conventions and namespacing</p>
</li>
<li>
<p>Since it is an internal API, it should be tightly coupled to the intended use and platform</p>
</li>
<li>
<p>API V2 should use some sort of view DSL to generate the JSON payloads</p>
</li>
<li>
<p>API V2 should be as predictable as possible in terms of typing and formatting.</p>
</li>
<li>
<p>API V2 should be super fast by default. It should be the last thing we need to optimize.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>So I looked at the serializer DSL that we had designed for API v1.
The first thing I did was to benchmark it against other solutions. So I stripped it out of legacy / no longer needed features, moved it to a local gem, and started playing with a few benchmarks.
And it was <em>very fast</em> indeed ! This is the result of a <a href="https://github.com/MoskitoHero/active_model_serializers/tree/benchmarks">benchmark</a> I did back then:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="shell">Warming up <span class="nt">--------------------------------------</span>
<span class="c"># Omitted for conciseness</span>
Calculating <span class="nt">-------------------------------------</span>
ams                    67.770  <span class="o">(</span>± 1.9%<span class="o">)</span> i/s -    657.000  <span class="k">in  </span>10.042270s
jsonapi-rb            157.239  <span class="o">(</span>± 2.3%<span class="o">)</span> i/s -      1.521k <span class="k">in  </span>10.010257s
barley                 96.909  <span class="o">(</span>± 2.4%<span class="o">)</span> i/s -    945.000  <span class="k">in  </span>10.036316s
barley-cache            4.589k <span class="o">(</span>± 2.8%<span class="o">)</span> i/s -     44.620k <span class="k">in  </span>10.010004s
ams          eager     68.237  <span class="o">(</span>± 6.2%<span class="o">)</span> i/s -    592.000  <span class="k">in  </span>10.087602s
jsonapi-rb   eager    289.733  <span class="o">(</span>± 3.7%<span class="o">)</span> i/s -      2.805k <span class="k">in  </span>10.033912s
barley       eager    406.732  <span class="o">(</span>± 3.2%<span class="o">)</span> i/s -      3.922k <span class="k">in  </span>10.020076s
barley-cache eager    639.935  <span class="o">(</span>± 2.4%<span class="o">)</span> i/s -      6.300k <span class="k">in  </span>10.053710s
with 95.0% confidence

Comparison:
barley-cache      :     4589.3 i/s
barley-cache eager:      639.9 i/s - 7.17x  <span class="o">(</span>± 0.27<span class="o">)</span> slower
barley       eager:      406.7 i/s - 11.29x  <span class="o">(</span>± 0.49<span class="o">)</span> slower
jsonapi-rb   eager:      289.7 i/s - 15.83x  <span class="o">(</span>± 0.74<span class="o">)</span> slower
jsonapi-rb        :      157.2 i/s - 29.17x  <span class="o">(</span>± 1.08<span class="o">)</span> slower
barley            :       96.9 i/s - 47.37x  <span class="o">(</span>± 1.75<span class="o">)</span> slower
ams          eager:       68.2 i/s - 67.25x  <span class="o">(</span>± 4.69<span class="o">)</span> slower
ams               :       67.8 i/s - 67.72x  <span class="o">(</span>± 2.31<span class="o">)</span> slower
with 95.0% confidence

Calculating <span class="nt">-------------------------------------</span>
<span class="c"># Omitted for conciseness</span>

Comparison:
barley-cache      :      46090 allocated
barley-cache eager:     216790 allocated - 4.70x more
barley       eager:     354326 allocated - 7.69x more
jsonapi-rb   eager:     694430 allocated - 15.07x more
jsonapi-rb        :     926082 allocated - 20.09x more
ams          eager:    1068038 allocated - 23.17x more
barley            :    1102354 allocated - 23.92x more
ams               :    1299674 allocated - 28.20x more</code></pre>
</div>
</div>
<div class="paragraph">
<p>Which is quite nice 😎.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="sharing-is-caring"><a class="anchor" href="#sharing-is-caring"></a>Sharing is caring</h2>
<div class="sectionbody">
<div class="paragraph">
<p>So I made a gem out of our serializer and <a href="https://github.com/MoskitoHero/barley">published it on Github</a>. It is named Barley.
I have recently made the gem even faster (from v0.9.0 on) and benchmarked it against <a href="https://github.com/okuramasafumi/alba">Alba</a>, <a href="https://github.com/procore-oss/blueprinter">Blueprinter</a>, <a href="https://github.com/malomalo/turbostreamer">Turbostreamer</a>, <a href="https://github.com/distil/jserializer">Jserializer</a>, <a href="https://github.com/estepnv/fast_serializer">FastSerializer</a>, <a href="https://github.com/trailblazer/representable">Representable</a>, <a href="https://github.com/vasilakisfil/SimpleAMS">SimpleAMS</a>, <a href="https://github.com/rails-api/active_model_serializers">AMS</a>, <a href="https://github.com/rails/jbuilder">Rails JBuilder</a> and <a href="https://github.com/yosiat/panko_serializer">Panko</a>.
Here are the results of these new benchmarks:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="shell">ruby 3.4.3 <span class="o">(</span>2025-04-14 revision d0b7e5b6a0<span class="o">)</span> +YJIT +PRISM <span class="o">[</span>x86_64-linux]
Warming up <span class="nt">--------------------------------------</span>
<span class="c"># Omitted for conciseness</span>

Calculating <span class="nt">-------------------------------------</span>
                alba    401.119 <span class="o">(</span>± 1.7%<span class="o">)</span> i/s    <span class="o">(</span>2.49 ms/i<span class="o">)</span> -      2.016k <span class="k">in   </span>5.027350s
alba_with_transformation
                        256.123 <span class="o">(</span>± 1.2%<span class="o">)</span> i/s    <span class="o">(</span>3.90 ms/i<span class="o">)</span> -      1.300k <span class="k">in   </span>5.076497s
         alba_inline     22.749 <span class="o">(</span>± 8.8%<span class="o">)</span> i/s   <span class="o">(</span>43.96 ms/i<span class="o">)</span> -    114.000 <span class="k">in   </span>5.049973s
                 ams     16.923 <span class="o">(</span>± 5.9%<span class="o">)</span> i/s   <span class="o">(</span>59.09 ms/i<span class="o">)</span> -     85.000 <span class="k">in   </span>5.029061s
              barley    427.045 <span class="o">(</span>± 2.6%<span class="o">)</span> i/s    <span class="o">(</span>2.34 ms/i<span class="o">)</span> -      2.162k <span class="k">in   </span>5.066130s
        barley_cache    378.285 <span class="o">(</span>± 2.4%<span class="o">)</span> i/s    <span class="o">(</span>2.64 ms/i<span class="o">)</span> -      1.927k <span class="k">in   </span>5.097462s
         blueprinter    120.704 <span class="o">(</span>± 1.7%<span class="o">)</span> i/s    <span class="o">(</span>8.28 ms/i<span class="o">)</span> -    612.000 <span class="k">in   </span>5.071371s
     fast_serializer    142.347 <span class="o">(</span>± 1.4%<span class="o">)</span> i/s    <span class="o">(</span>7.03 ms/i<span class="o">)</span> -    720.000 <span class="k">in   </span>5.058924s
         jserializer    252.455 <span class="o">(</span>± 1.6%<span class="o">)</span> i/s    <span class="o">(</span>3.96 ms/i<span class="o">)</span> -      1.274k <span class="k">in   </span>5.047584s
               panko    510.262 <span class="o">(</span>± 4.7%<span class="o">)</span> i/s    <span class="o">(</span>1.96 ms/i<span class="o">)</span> -      2.585k <span class="k">in   </span>5.077381s
               rails    107.589 <span class="o">(</span>± 7.4%<span class="o">)</span> i/s    <span class="o">(</span>9.29 ms/i<span class="o">)</span> -    546.000 <span class="k">in   </span>5.104715s
       representable     50.264 <span class="o">(</span>± 6.0%<span class="o">)</span> i/s   <span class="o">(</span>19.89 ms/i<span class="o">)</span> -    252.000 <span class="k">in   </span>5.027987s
          simple_ams     36.584 <span class="o">(</span>± 5.5%<span class="o">)</span> i/s   <span class="o">(</span>27.33 ms/i<span class="o">)</span> -    184.000 <span class="k">in   </span>5.054347s
       turbostreamer    351.551 <span class="o">(</span>± 1.7%<span class="o">)</span> i/s    <span class="o">(</span>2.84 ms/i<span class="o">)</span> -      1.776k <span class="k">in   </span>5.053401s

Comparison:
               panko:      510.3 i/s
              barley:      427.0 i/s - 1.19x  slower
                alba:      401.1 i/s - 1.27x  slower
        barley_cache:      378.3 i/s - 1.35x  slower
       turbostreamer:      351.6 i/s - 1.45x  slower
alba_with_transformation:      256.1 i/s - 1.99x  slower
         jserializer:      252.5 i/s - 2.02x  slower
     fast_serializer:      142.3 i/s - 3.58x  slower
         blueprinter:      120.7 i/s - 4.23x  slower
               rails:      107.6 i/s - 4.74x  slower
       representable:       50.3 i/s - 10.15x  slower
          simple_ams:       36.6 i/s - 13.95x  slower
         alba_inline:       22.7 i/s - 22.43x  slower
                 ams:       16.9 i/s - 30.15x  slower

Calculating <span class="nt">-------------------------------------</span>
<span class="c"># Omitted for conciseness</span>

Comparison:
               panko:     259178 allocated
              barley:     633521 allocated - 2.44x more
       turbostreamer:     641760 allocated - 2.48x more
alba_with_transformation:     818181 allocated - 3.16x more
                alba:     818241 allocated - 3.16x more
         jserializer:     822281 allocated - 3.17x more
        barley_cache:     849521 allocated - 3.28x more
     fast_serializer:    1470121 allocated - 5.67x more
         blueprinter:    2297921 allocated - 8.87x more
         alba_inline:    2736041 allocated - 10.56x more
               rails:    2757857 allocated - 10.64x more
                 ams:    4713401 allocated - 18.19x more
       representable:    5151321 allocated - 19.88x more
          simple_ams:    9017033 allocated - 34.79x more</code></pre>
</div>
</div>
<div class="paragraph">
<p>With YJIT enabled, Barley really shines and is the first Hash serializer of the group.
There are <a href="https://github.com/MoskitoHero/barley/tree/main/benchmarks#readme">more benchmarks to be found in the Barley repository</a>.
Barley is very simple in its implementation and allocates very little. I believe that it is what makes it so fast. I’ll write another post on how I optimized it in version 0.9.0.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="the-features"><a class="anchor" href="#the-features"></a>The Features</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Barley provides:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a clear DSL to define your serializer</p>
</li>
<li>
<p>inline / block sub-serializer definition</p>
</li>
<li>
<p>russian-doll caching</p>
</li>
<li>
<p>type-checking with <a href="https://dry-rb.org/gems/dry-types/main/">dry-types</a></p>
</li>
<li>
<p>context handling</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This is how Barley works:</p>
</div>
<div class="sect2">
<h3 id="in-the-model-you-want-to-serialize"><a class="anchor" href="#in-the-model-you-want-to-serialize"></a>In the model you want to serialize</h3>
<div class="paragraph">
<p>First, include the Barley::Serializable concern in your model.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># /app/models/user.rb</span>
<span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="kp">include</span> <span class="no">Barley</span><span class="o">::</span><span class="no">Serializable</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Once you include this module, Barley will look for a UserSerializable class in your codebase, so you need to define one, typically in the app/serializable directory. It should inherit from Barley::Serializer</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># /app/serializers/user_serializer.rb</span>
<span class="k">class</span> <span class="nc">UserSerializer</span> <span class="o">&lt;</span> <span class="no">Barley</span><span class="o">::</span><span class="no">Serializer</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Once this is set up, the as_json method on your model will return what you have defined in your serializer class.
You can also chose to use a different serializer to render your data. You can use the serializer method in your model. This method also allows you to pass a cache argument.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># /app/models/user.rb</span>
<span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="kp">include</span> <span class="no">Barley</span><span class="o">::</span><span class="no">Serializable</span>

  <span class="n">serializer</span> <span class="no">MyCustomSerializer</span><span class="p">,</span> <span class="ss">cache: </span><span class="kp">true</span>
  <span class="c1"># or</span>
  <span class="n">serializer</span> <span class="no">MyCustomSerializer</span><span class="p">,</span> <span class="ss">cache: </span><span class="p">{</span><span class="ss">expires_in: </span><span class="mi">1</span><span class="p">.</span><span class="nf">hour</span><span class="p">}</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Another way to achieve this is through the as_json method:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="no">User</span><span class="p">.</span><span class="nf">first</span><span class="p">.</span><span class="nf">as_json</span><span class="p">(</span><span class="ss">serializer: </span><span class="no">MyCustomSerializer</span><span class="p">,</span> <span class="ss">cache: </span><span class="p">{</span><span class="ss">expires_in: </span><span class="mi">1</span><span class="p">.</span><span class="nf">hour</span><span class="p">})</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Please note that Barley overrides the as_json method and does not provide it with standard only, include or except arguments. This is because the purpose of this serializer is precisely to do that job. The root argument works, though.</p>
</div>
</div>
<div class="sect2">
<h3 id="defining-the-serializer"><a class="anchor" href="#defining-the-serializer"></a>Defining the serializer</h3>
<div class="paragraph">
<p>Let’s go back to the serializer class. Barley provides a simple DSL that allows you to define your JSON in a readable and maintainable way. Here is an example definition that covers most use cases:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># /app/serializers/user_serializer.rb</span>
<span class="k">class</span> <span class="nc">UserSerializer</span> <span class="o">&lt;</span> <span class="no">Barley</span><span class="o">::</span><span class="no">Serializer</span>

  <span class="n">attributes</span> <span class="ss">id: </span><span class="no">Types</span><span class="o">::</span><span class="no">Strict</span><span class="o">::</span><span class="no">Integer</span><span class="p">,</span> <span class="ss">:name</span>

  <span class="n">attribute</span> <span class="ss">:email</span>
  <span class="n">attribute</span> <span class="ss">:value</span><span class="p">,</span> <span class="ss">type: </span><span class="no">Types</span><span class="o">::</span><span class="no">Coercible</span><span class="o">::</span><span class="no">Integer</span>

  <span class="n">many</span> <span class="ss">:posts</span>

  <span class="n">one</span> <span class="ss">:group</span><span class="p">,</span> <span class="ss">serializer: </span><span class="no">CustomGroupSerializer</span>

  <span class="n">many</span> <span class="ss">:related_users</span><span class="p">,</span> <span class="ss">key: :friends</span><span class="p">,</span> <span class="ss">cache: </span><span class="kp">true</span>

  <span class="n">one</span> <span class="ss">:profile</span><span class="p">,</span> <span class="ss">cache: </span><span class="p">{</span> <span class="ss">expires_in: </span><span class="mi">1</span><span class="p">.</span><span class="nf">day</span> <span class="p">}</span> <span class="k">do</span>
    <span class="n">attributes</span> <span class="ss">:avatar</span><span class="p">,</span> <span class="ss">:social_url</span>

    <span class="n">attribute</span> <span class="ss">:badges</span> <span class="k">do</span>
      <span class="n">object</span><span class="p">.</span><span class="nf">badges</span><span class="p">.</span><span class="nf">map</span><span class="p">(</span><span class="o">&amp;</span><span class="ss">:display_name</span><span class="p">)</span>
    <span class="k">end</span>
  <span class="k">end</span>

<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>For type-checking to work, you need to define a Types module like this, somewhere in your codebase:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">module</span> <span class="nn">Types</span>
  <span class="kp">include</span> <span class="no">Dry</span><span class="o">.</span><span class="no">Types</span><span class="p">()</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="sect3">
<h4 id="1-attributes"><a class="anchor" href="#1-attributes"></a>1. Attributes</h4>
<div class="paragraph">
<p>They can be defined as an array with the plural attributes method. It can be an array of symbols, or an array of hashes in the key: TypeDefinition pattern, or a mix of both.
They can also be defined with a singular attribute method, that accepts a type keyword argument.
If type is included in an attribute’s definition, it will raise an error. You can use types to coerce types and define constraints. Please refer to the <a href="https://dry-rb.org/gems/dry-types/main/">dry-types</a> gem for more options.
If you give a block to the attribute method, you can also return anything you like. You have the object object at hand that refers to the serialized model.</p>
</div>
</div>
<div class="sect3">
<h4 id="2-associations"><a class="anchor" href="#2-associations"></a>2. Associations</h4>
<div class="paragraph">
<p>They are defined with the one or many methods.
It will use the default serializer if no argument is given. You can provide it with a serializer and a cache option.
These methods do not provide a type argument, as it makes no sense to do so.
You can also decide to give it a block, that allows you to define the association’s serializer inline.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="wrapping-it-up"><a class="anchor" href="#wrapping-it-up"></a>Wrapping it up</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The Barley gem’s goal is to provide a simple DSL to define your JSON payloads with a type-proof option, and render it in a very fast and efficient manner. It is probably not as feature-packed as other serializers available to the rails community, but it is a no-brainer, yet powerful option that you should consider if you are looking for performance and simplicity.
I’d be happy to see more projects starting to adopt it and would love the community to provide optimizations to make it even better.</p>
</div>
</div>
</div>
]]></content:encoded></item><item><title>Track and debug your ActiveRecord SQL queries with annotations</title><link>/post/2024-12-10-annotating-sql-queries-for-debugging-purposes/</link><pubDate>Tue, 10 Dec 2024 00:00:00 +0000</pubDate><guid>/post/2024-12-10-annotating-sql-queries-for-debugging-purposes/</guid><description>Embed contextual comments directly into your SQL queries using ActiveRecord’s annotate method, making log analysis and debugging far easier.</description><content:encoded><![CDATA[<div class="paragraph">
<p>I recently <a href="https://blog.saeloun.com/2020/01/29/rails-6-adds-activerecord-relation-annotate-for-adding-sql-comments-to-activerecord-relation-queries/">discovered</a> a handy feature <a href="https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-annotate">introduced in Rails 6</a>. This feature allows you to annotate your SQL queries with comments directly embedded in the query itself. I know, I’m a bit late to the party, but I suspect I’m not the only one, so here’s a quick rundown.</p>
</div>
<div class="paragraph">
<p>In our app, we have a deeply nested hash called <code>public_data</code> that’s often used to render a record’s JSON payload, along with its associations, in the controller.</p>
</div>
<div class="paragraph">
<p>This <code>public_data</code> hash is built from a mix of attributes and model methods. However, it also calls the <code>public_data</code> methods of associated models, which might, in turn, call back to the original model’s <code>public_data</code>. This circular complexity has been a challenge, and while we’ve started transitioning to the <a href="https://github.com/MoskitoHero/barley">Barley serializer gem</a> for better structure, we still rely on this method in parts of our codebase for backward compatibility.</p>
</div>
<div class="paragraph">
<p>So when <code>@user.public_data</code> is called in the controller, it often triggers a long chain of database queries. Tracking down where a specific relation is being called—​whether in <code>User</code>, <code>Profile</code>, or <code>Subscription</code>--can be daunting.</p>
</div>
<div class="paragraph">
<p>While Rails logs are helpful, they require sifting through the output to correlate the log entry with the SQL query. This becomes even trickier when debugging in remote environments and searching through extensive logs in tools like New Relic or CloudWatch.</p>
</div>
<div class="paragraph">
<p>Enter <code>ActiveRecord::Relation#annotate</code>…​</p>
</div>
<div class="paragraph">
<p>This simple method lets you add comments to your SQL queries, embedding contextual information directly within the query.</p>
</div>
<div class="paragraph">
<p>Here’s how it works:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">users</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">active</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="ss">score: </span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">annotate</span><span class="p">(</span><span class="s2">&#34;called from Admin::UsersController&#34;</span><span class="p">)</span>

<span class="c1">#&gt; SELECT * FROM `users` WHERE `last_visit_at` &gt;= &#39;2024-11-10 16:53:05&#39;</span>
<span class="c1">#  AND `score` &lt;= 5 /* called from Admin::UsersController */ ORDER BY `id` ASC</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>This provides a clear indication of where the query originated. You can also include additional context, such as the current locale, user, or order ID, depending on your needs.</p>
</div>
<div class="paragraph">
<p>For example, adding an annotation inside the <code>public_data</code> method on my <code>Subscription</code> model makes it much easier to trace where the query originated in the chain.</p>
</div>
<div class="paragraph">
<p>Another great feature is the ability to stack annotations. Let’s say we add an annotation in the <code>active</code> scope of the <code>User</code> model:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="n">scope</span> <span class="ss">:active</span><span class="p">,</span> <span class="o">-&gt;</span> <span class="p">{</span> <span class="n">where</span><span class="p">(</span><span class="ss">last_visit_at: </span><span class="mi">1</span><span class="p">.</span><span class="nf">week</span><span class="p">.</span><span class="nf">ago</span><span class="o">..</span><span class="p">).</span><span class="nf">annotate</span><span class="p">(</span><span class="s2">&#34;user active in the past week&#34;</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Calling the same query as before would generate:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">users</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">active</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="ss">score: </span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">annotate</span><span class="p">(</span><span class="s2">&#34;called from Admin::UsersController&#34;</span><span class="p">)</span>

<span class="c1">#&gt; SELECT * FROM `users` WHERE `last_visit_at` &gt;= &#39;2024-11-10 16:53:05&#39;</span>
<span class="c1">#  AND `score` &lt;= 5 /* user active in the past week */ /* called from Admin::UsersController */</span>
<span class="c1">#  ORDER BY `id` ASC</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Neat, right? This approach brings more clarity and structure to your logs, making them easier to query and group in log viewers. Additionally, these annotations appear in your database logs, offering a seamless way to correlate them with application logs.</p>
</div>
<div class="paragraph">
<p>Possible applications include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Tracking queries in background jobs</p>
</li>
<li>
<p>Tagging controller context, headers, or anything else inside the queries</p>
</li>
<li>
<p>Debugging N+1 or slow queries</p>
</li>
<li>
<p>Expliciting the scope of queries</p>
</li>
<li>
<p>And so much more creative stuff!</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>That said, annotations come with a slight tradeoff in query readability. It’s essential to be deliberate about what and when to log, and to periodically review whether the annotations remain relevant and valuable.</p>
</div>
<div class="paragraph">
<p>Even if used exclusively for local debugging, SQL annotations are an excellent addition to your toolbox.</p>
</div>
]]></content:encoded></item></channel></rss>