<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://www.mintbit.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.mintbit.com/" rel="alternate" type="text/html" /><updated>2026-08-11T03:11:24-04:00</updated><id>https://www.mintbit.com/feed.xml</id><title type="html">Mintbit</title><subtitle>Mintbit is a Ruby on Rails consultancy and venture studio based in Knoxville, Tennessee with a focus on software development and workflow automation.</subtitle><entry><title type="html">The Hidden Cost of Callbacks</title><link href="https://www.mintbit.com/blog/the-hidden-cost-of-callbacks/" rel="alternate" type="text/html" title="The Hidden Cost of Callbacks" /><published>2026-02-27T12:52:00-05:00</published><updated>2026-02-27T12:52:00-05:00</updated><id>https://www.mintbit.com/blog/the-hidden-cost-of-callbacks</id><content type="html" xml:base="https://www.mintbit.com/blog/the-hidden-cost-of-callbacks/"><![CDATA[<p>In the beginning, <a href="https://guides.rubyonrails.org/active_record_callbacks.html">Rails callbacks</a> like <a href="https://api.rubyonrails.org/v8.1.2/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_save">after_save</a> or <a href="https://api.rubyonrails.org/v8.1.2/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_commit">after_commit</a> feel like magic. You save a user, and—<em>poof</em>—a welcome email is sent. It’s easy, it’s fast, and it keeps your controller clean.</p>

<p>But as your application grows, these “invisible” side effects become a maintenance nightmare. Today, we’ll explore why callbacks are often technical debt in disguise and how to move toward a more explicit, modern architecture.</p>

<h2 id="1-the-problem-the-side-effect-trap">1. The Problem: The “Side Effect” Trap</h2>

<p>The biggest issue with callbacks is that they are <strong>implicit</strong>. When you call <code class="language-ruby highlighter-rouge"><span class="n">user</span><span class="p">.</span><span class="nf">save</span></code>, you expect a database write. You don’t necessarily expect a 3rd-party API call to Stripe, a background job enqueued to Intercom, and a cache purge for the entire dashboard.</p>

<h3 id="why-this-hurts">Why this hurts:</h3>

<ul>
  <li><strong>Testing Hell:</strong> Your unit tests become slow because every <code class="language-ruby highlighter-rouge"><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span></code> triggers a chain reaction of logic that isn’t relevant to the test at hand.</li>
  <li><strong>Fragility:</strong> Changing a minor field might trigger a callback that fails, preventing the entire record from saving.</li>
  <li><strong>Circular Dependencies:</strong> Object A saves Object B, which has a callback to update Object A… and suddenly you have an infinite loop.</li>
</ul>

<h2 id="2-the-alternative-explicit-service-objects">2. The Alternative: Explicit Service Objects</h2>

<p>The modern Rails way is to favor <strong>Explicit over Implicit</strong>. Instead of hiding logic inside the Model, move it to a dedicated <strong>Service Object</strong>. This makes the flow of data obvious and easy to test in isolation.</p>

<h3 id="the-callback-way-hiding-the-logic">The Callback Way (Hiding the logic):</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="n">after_create</span> <span class="ss">:send_welcome_email</span>

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

  <span class="k">def</span> <span class="nf">send_welcome_email</span>
    <span class="no">UserMailer</span><span class="p">.</span><span class="nf">welcome</span><span class="p">(</span><span class="nb">self</span><span class="p">).</span><span class="nf">deliver_later</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h3 id="the-service-object-way-explicit-logic">The Service Object Way (Explicit logic):</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">UserRegistrationService</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">call</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
    <span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
    
    <span class="k">if</span> <span class="n">user</span><span class="p">.</span><span class="nf">save</span>
      <span class="no">UserMailer</span><span class="p">.</span><span class="nf">welcome</span><span class="p">(</span><span class="n">user</span><span class="p">).</span><span class="nf">deliver_later</span>
      <span class="no">Analytics</span><span class="p">.</span><span class="nf">track</span><span class="p">(</span><span class="s2">"User Signed Up"</span><span class="p">,</span> <span class="ss">user_id: </span><span class="n">user</span><span class="p">.</span><span class="nf">id</span><span class="p">)</span>
    <span class="k">end</span>
    
    <span class="n">user</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Now, when you look at the service, you know <strong>exactly</strong> what happens when a user registers. No surprises.</p>

<h2 id="3-modern-observers-activesupportnotifications">3. Modern Observers: ActiveSupport::Notifications</h2>

<p>If you truly need a “decoupled” way to handle side effects without bloating your service objects, the modern “Observer” pattern in Rails is <a href="https://api.rubyonrails.org/v8.0/classes/ActiveSupport/Notifications.html">ActiveSupport::Notifications</a>.</p>

<p>This follows the <strong>Pub/Sub (Publish/Subscribe)</strong> pattern. The Model (or Service) simply broadcasts that something happened, and “Subscribers” listen and react.</p>

<h3 id="step-1-broadcast-the-event">Step 1: Broadcast the event</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="k">def</span> <span class="nf">register</span>
    <span class="k">if</span> <span class="n">save</span>
      <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Notifications</span><span class="p">.</span><span class="nf">instrument</span><span class="p">(</span><span class="s2">"user.registered"</span><span class="p">,</span> <span class="ss">user: </span><span class="nb">self</span><span class="p">)</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h3 id="step-2-subscribe-to-the-event-the-modern-observer">Step 2: Subscribe to the event (The Modern Observer)</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="c1"># config/initializers/events.rb</span>
<span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Notifications</span><span class="p">.</span><span class="nf">subscribe</span><span class="p">(</span><span class="s2">"user.registered"</span><span class="p">)</span> <span class="k">do</span> <span class="o">|*</span><span class="n">args</span><span class="o">|</span>
  <span class="n">event</span> <span class="o">=</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Notifications</span><span class="o">::</span><span class="no">Event</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  <span class="n">user</span> <span class="o">=</span> <span class="n">event</span><span class="p">.</span><span class="nf">payload</span><span class="p">[</span><span class="ss">:user</span><span class="p">]</span>
  
  <span class="no">UserMailer</span><span class="p">.</span><span class="nf">welcome</span><span class="p">(</span><span class="n">user</span><span class="p">).</span><span class="nf">deliver_later</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="use-callbacks-for-data-services-for-business">Use Callbacks for Data, Services for Business</h2>

<p>Are callbacks always evil? No.</p>

<ul>
  <li><strong>Use Callbacks for:</strong> Internal data integrity (e.g., downcasing an email, setting a slug).</li>
  <li><strong>Avoid Callbacks for:</strong> Business logic, sending emails, hitting APIs, or updating unrelated models.</li>
</ul>

<p>By moving side effects out of your models, your test suite will get faster, and your code will become much easier to reason about.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[In the beginning, Rails callbacks like after_save or after_commit feel like magic. You save a user, and—poof—a welcome email is sent. It’s easy, it’s fast, and it keeps your controller clean.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/the-hidden-cost-of-callbacks.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/the-hidden-cost-of-callbacks.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Ruby Version Managers</title><link href="https://www.mintbit.com/blog/ruby-version-managers/" rel="alternate" type="text/html" title="Ruby Version Managers" /><published>2026-02-10T08:11:00-05:00</published><updated>2026-02-10T08:11:00-05:00</updated><id>https://www.mintbit.com/blog/ruby-version-managers</id><content type="html" xml:base="https://www.mintbit.com/blog/ruby-version-managers/"><![CDATA[<p>Choosing the right version manager is often the first “real” challenge for a Ruby on Rails developer. It’s not just about installing the language; it’s about ensuring that your development environment matches your production server and that you can switch between legacy projects and the latest features without breaking your machine.</p>

<p>In this post, we’ll explore the main version managers in the ecosystem and a significant shift introduced in <strong>Rails 8</strong> regarding environment consistency.</p>

<h2 id="why-use-a-version-manager">Why Use a Version Manager?</h2>

<p>Ruby is an evolving language. Different projects often require different versions of Ruby and Rails. If you install Ruby directly through your operating system’s package manager (like <code class="language-ruby highlighter-rouge"><span class="n">apt</span></code> or <code class="language-ruby highlighter-rouge"><span class="n">brew</span></code>), you are stuck with one version, and you risk breaking system tools that rely on that specific version.</p>

<p>Version managers solve this by:</p>
<ul>
  <li>Allowing multiple Ruby versions to coexist.</li>
  <li>Installing Gems separately for each Ruby version.</li>
  <li>Switching versions automatically when you enter a project folder.</li>
</ul>

<h2 id="the-main-contenders">The Main Contenders</h2>

<h3 id="1-mise-the-modern-speedster">1. mise (The Modern Speedster)</h3>
<p>Formerly known as <code class="language-ruby highlighter-rouge"><span class="n">rtx</span></code>, <a href="https://mise.jdx.dev/">mise</a> is the new community favorite. It is a drop-in replacement for <code class="language-ruby highlighter-rouge"><span class="n">asdf</span></code> but written in Rust, making it significantly faster.</p>

<ul>
  <li><strong>Pros:</strong> Compatible with <code class="language-ruby highlighter-rouge"><span class="n">asdf</span></code> plugins and <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">tool</span><span class="o">-</span><span class="n">versions</span></code>; manages Ruby, Node, and env vars; <strong>extremely fast</strong>.</li>
  <li><strong>Cons:</strong> Newer than others, but growing rapidly.</li>
</ul>

<h3 id="2-asdf-the-versatile-choice">2. asdf (The Versatile Choice)</h3>
<p>Until recently the gold standard for polyglot developers. <a href="https://asdf-vm.com/">asdf</a> uses a plugin system to manage almost any runtime (Ruby, Node.js, PostgreSQL).</p>

<ul>
  <li><strong>Pros:</strong> One tool for your entire stack; uses a single <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">tool</span><span class="o">-</span><span class="n">versions</span></code> file.</li>
  <li><strong>Cons:</strong> Being replaced by <code class="language-ruby highlighter-rouge"><span class="n">mise</span></code> due to speed and ease of use.</li>
</ul>

<h3 id="3-rbenv-the-minimalist">3. rbenv (The Minimalist)</h3>
<p>Lightweight and focused solely on Ruby. It works by using “shims” to intercept Ruby commands.</p>

<ul>
  <li><strong>Pros:</strong> Very stable; doesn’t interfere with your shell as much as others.</li>
  <li><strong>Cons:</strong> Requires the <code class="language-ruby highlighter-rouge"><span class="n">ruby</span><span class="o">-</span><span class="n">build</span></code> plugin to actually install new versions.</li>
</ul>

<h3 id="4-rvm-the-veteran">4. rvm (The Veteran)</h3>
<p>The oldest and most feature-rich. It manages Ruby versions and “gemsets” (isolated silos for gems).</p>

<ul>
  <li><strong>Pros:</strong> Very powerful; handles everything out of the box.</li>
  <li><strong>Cons:</strong> Overrides many shell commands, which can lead to conflicts with modern tools.</li>
</ul>

<h2 id="how-it-works-the-ruby-version-file">How it works: The .ruby-version File</h2>

<p>Most version managers respect a file called <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">ruby</span><span class="o">-</span><span class="n">version</span></code> placed in your project’s root directory. When you <code class="language-ruby highlighter-rouge"><span class="n">cd</span></code> into the folder, the manager reads this file and switches the Ruby version for you automatically.</p>

<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre># .ruby-version
3.3.0
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="the-rails-8-shift-beyond-the-version-manager">The Rails 8 Shift: Beyond the Version Manager</h2>

<p>With the release of <strong>Rails 8</strong>, the conversation about version managers has shifted slightly. While you still need a way to install Ruby locally, Rails 8 doubles down on <strong>containerization</strong> as the primary way to manage environment consistency through <strong>Kamal</strong> and an improved <strong>Docker</strong> integration.</p>

<h3 id="dev-containers-in-rails-8">Dev Containers in Rails 8</h3>

<p>Rails 8 makes it easier than ever to use <strong>Dev Containers</strong>. Instead of worrying if every developer on your team has the right version of <code class="language-ruby highlighter-rouge"><span class="n">mise</span></code> or <code class="language-ruby highlighter-rouge"><span class="n">rbenv</span></code>, Rails 8 provides a pre-configured <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">devcontainer</span></code> folder.</p>

<ul>
  <li><strong>Standardized Environment:</strong> The Ruby version, database, and Redis are all defined in a Docker image.</li>
  <li><strong>Zero-Setup:</strong> A new developer can clone the repo and open it in VS Code, and the entire environment is spun up inside a container.</li>
</ul>

<h3 id="kamal-moving-to-production">Kamal: Moving to Production</h3>

<p>Rails 8 uses <strong>Kamal</strong> by default for deployment. Kamal packages your app into a Docker image. This means that the version of Ruby you used in development is <em>exactly</em> the same one that goes to production, reducing the “it works on my machine” syndrome to zero.</p>

<p>If you are a modern developer working on multiple languages and want the best performance, <strong>mise</strong> is the current winner. If you prefer the classic, stable approach, <strong>rbenv</strong> remains a solid choice.</p>

<p>However, keep an eye on the <strong>Rails 8</strong> philosophy: while version managers are great for your local shell, <strong>Docker and Dev Containers</strong> are becoming the standard for professional, reproducible development environments.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[Choosing the right version manager is often the first “real” challenge for a Ruby on Rails developer. It’s not just about installing the language; it’s about ensuring that your development environment matches your production server and that you can switch between legacy projects and the latest features without breaking your machine.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/ruby-version-managers.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/ruby-version-managers.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ERB vs. Phlex: Choosing the Right View Strategy for Your Rails App</title><link href="https://www.mintbit.com/blog/erb-vs-phlex-choosing-the-right-view-strategy-for-your-rails-app/" rel="alternate" type="text/html" title="ERB vs. Phlex: Choosing the Right View Strategy for Your Rails App" /><published>2026-01-29T10:47:00-05:00</published><updated>2026-01-29T10:47:00-05:00</updated><id>https://www.mintbit.com/blog/erb-vs-phlex-choosing-the-right-view-strategy-for-your-rails-app</id><content type="html" xml:base="https://www.mintbit.com/blog/erb-vs-phlex-choosing-the-right-view-strategy-for-your-rails-app/"><![CDATA[<p>The Ruby on Rails community is currently at a crossroads regarding the “View” layer. On one side, we have <strong>ERB (Embedded Ruby)</strong>, the battle-tested veteran that has powered Rails since day one. On the other, <strong>Phlex</strong> has emerged as a disruptive alternative that treats HTML as pure Ruby code.</p>

<p>Both approaches are valid, but they favor different mental models and workflows. Here is a breakdown of the pros and cons of each to help you decide which fits your project best.</p>

<h2 id="1-erb-embedded-ruby">1. ERB (Embedded Ruby)</h2>

<p>ERB is a template-based system. It feels like writing an HTML document that has “holes” where Ruby code can be injected.</p>

<h3 id="the-pros">The Pros</h3>

<ul>
  <li><strong>HTML-First Mindset:</strong> Because it looks like HTML, it is very accessible to frontend developers and designers who may not be proficient in Ruby.</li>
  <li><strong>Standard Framework Support:</strong> Every Rails tool, tutorial, and gem is built with ERB in mind. It requires zero configuration.</li>
  <li><strong>Low Barrier to Entry:</strong> Beginners can start building views immediately without understanding Object-Oriented Programming (OOP) principles.</li>
</ul>

<h3 id="the-cons">The Cons</h3>

<ul>
  <li><strong>Fragmentation:</strong> Logic often ends up scattered between <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">html</span><span class="p">.</span><span class="nf">erb</span></code> files, global <a href="https://guides.rubyonrails.org/action_view_helpers.html">helpers</a>, and <a href="https://guides.rubyonrails.org/layouts_and_rendering.html">partials</a>.</li>
  <li><strong>Loose Contracts:</strong> Partials don’t have a formal way to define required arguments, often leading to “undefined local variable” errors at runtime.</li>
  <li><strong>Performance:</strong> Parsing and interpolating large strings in ERB is slower than executing compiled Ruby methods.</li>
</ul>

<h2 id="2-phlex">2. Phlex</h2>

<p>Phlex is a component-based system. It feels like writing a Ruby class that happens to output HTML.</p>

<h3 id="the-pros-1">The Pros</h3>

<ul>
  <li><strong>Object-Oriented Power:</strong> You get the full benefit of Ruby: private methods for refactoring, constants for CSS classes, and inheritance.</li>
  <li><strong>Type Safety &amp; Contracts:</strong> By using the standard <code class="language-ruby highlighter-rouge"><span class="n">initialize</span></code> method, you define exactly what data a component needs. If you miss an argument, Ruby tells you exactly why and where.</li>
  <li><strong>Speed:</strong> Phlex is optimized for high performance, often outperforming ERB and even other component gems like ViewComponent.</li>
  <li><strong>Developer Experience:</strong> Everything is in one place. You don’t have to jump between a helper file and a template to understand a component’s logic.</li>
</ul>

<h3 id="the-cons-1">The Cons</h3>

<ul>
  <li><strong>The “Ruby Barrier”:</strong> Designers who only know HTML/CSS will find Phlex intimidating, as the HTML structure is abstracted into Ruby method calls.</li>
  <li><strong>Non-Standard Syntax:</strong> While it mirrors HTML, you are writing <code class="language-ruby highlighter-rouge"><span class="n">div</span> <span class="p">{</span> <span class="o">...</span> <span class="p">}</span></code> instead of <code class="language-ruby highlighter-rouge"><span class="o">&lt;</span><span class="n">div</span><span class="o">&gt;...&lt;</span><span class="sr">/div&gt;</span></code>. This requires a mental shift and can occasionally lead to syntax confusion.</li>
</ul>

<h2 id="the-side-by-side-user-card-component">The Side-by-Side: User Card Component</h2>

<p>To see the difference in “feeling,” consider this comparison:</p>

<h3 id="erb-implementation">ERB Implementation</h3>

<div class="language-erb highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"user-card"</span><span class="nt">&gt;</span>
  <span class="cp">&lt;%=</span> <span class="n">image_tag</span> <span class="vi">@user</span><span class="p">.</span><span class="nf">avatar</span> <span class="cp">%&gt;</span>
  <span class="nt">&lt;h2&gt;</span><span class="cp">&lt;%=</span> <span class="vi">@user</span><span class="p">.</span><span class="nf">name</span> <span class="cp">%&gt;</span><span class="nt">&lt;/h2&gt;</span>
  <span class="cp">&lt;%</span> <span class="k">if</span> <span class="vi">@user</span><span class="p">.</span><span class="nf">admin?</span> <span class="cp">%&gt;</span>
    <span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">"badge"</span><span class="nt">&gt;</span>Admin<span class="nt">&lt;/span&gt;</span>
  <span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h3 id="phlex-implementation">Phlex Implementation</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">view_template</span>
  <span class="n">div</span><span class="p">(</span><span class="ss">class: </span><span class="s2">"user-card"</span><span class="p">)</span> <span class="k">do</span>
    <span class="n">image_tag</span><span class="p">(</span><span class="vi">@user</span><span class="p">.</span><span class="nf">avatar</span><span class="p">)</span>
    <span class="n">h2</span> <span class="p">{</span> <span class="vi">@user</span><span class="p">.</span><span class="nf">name</span> <span class="p">}</span>
    <span class="n">span</span><span class="p">(</span><span class="ss">class: </span><span class="s2">"badge"</span><span class="p">)</span> <span class="p">{</span> <span class="s2">"Admin"</span> <span class="p">}</span> <span class="k">if</span> <span class="vi">@user</span><span class="p">.</span><span class="nf">admin?</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="which-one-should-you-choose">Which One Should You Choose?</h2>

<h3 id="choose-erb-if">Choose ERB if:</h3>

<ul>
  <li>You work in a team with <strong>dedicated frontend developers</strong> or designers who need to edit templates directly.</li>
  <li>You are building a <strong>content-heavy site</strong> where the HTML structure is more important than the logic.</li>
  <li>You want to stick to <strong>the Rails default</strong> to ensure maximum compatibility with third-party gems.</li>
</ul>

<h3 id="choose-phlex-if">Choose Phlex if:</h3>

<ul>
  <li>You are a <strong>solo developer or a Ruby-heavy team</strong> that wants to stay within the Ruby language as much as possible.</li>
  <li>You are building a <strong>complex, data-driven application</strong> with many reusable UI components.</li>
  <li><strong>Performance</strong> is a top priority for your rendering layer.</li>
  <li>You find yourself frustrated by the “spaghetti” logic of helpers and partials.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[The Ruby on Rails community is currently at a crossroads regarding the “View” layer. On one side, we have ERB (Embedded Ruby), the battle-tested veteran that has powered Rails since day one. On the other, Phlex has emerged as a disruptive alternative that treats HTML as pure Ruby code.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/erb-vs-phlex-choosing-the-right-view-strategy-for-your-rails-app.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/erb-vs-phlex-choosing-the-right-view-strategy-for-your-rails-app.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Rails Console: Making reload! Truly Reliable</title><link href="https://www.mintbit.com/blog/rails-console-making-reload-truly-reliable/" rel="alternate" type="text/html" title="Rails Console: Making reload! Truly Reliable" /><published>2026-01-29T10:39:00-05:00</published><updated>2026-01-29T10:39:00-05:00</updated><id>https://www.mintbit.com/blog/rails-console-making-reload-truly-reliable</id><content type="html" xml:base="https://www.mintbit.com/blog/rails-console-making-reload-truly-reliable/"><![CDATA[<p>For any Rails developer, the <code class="language-ruby highlighter-rouge"><span class="n">reload!</span></code> command is a lifeline. Whether you’re testing a new method or debugging a complex query, the ability to refresh the application code without restarting the console session is a massive productivity boost.</p>

<p>However, as the framework has evolved to become more robust, a subtle discrepancy appeared: while your <strong>code</strong> was reloading, the <strong>execution state</strong> (such as the Query Cache) sometimes stayed stuck in the past. A recent update to Rails, implemented in <a href="https://github.com/rails/rails/pull/56639">Pull Request #56639</a>, fixes this by making the console experience more consistent with how the framework handles actual web requests.</p>

<h2 id="the-invisible-shield-the-rails-executor">The Invisible Shield: The Rails Executor</h2>

<p>In modern Rails versions, code execution is wrapped in what is known as the <strong>Rails Executor</strong>. This component manages the “lifecycle” of a task, handling essential housekeeping such as:</p>

<ul>
  <li>Clearing and resetting the Query Cache.</li>
  <li>Returning database connections to the pool.</li>
  <li>Managing Zeitwerk’s code loading state.</li>
</ul>

<p>Recently, the Rails console began using the Executor for every command to mirror a real web request environment. But this introduced a catch: the Executor’s state could persist longer than intended during a long-running console session.</p>

<h2 id="the-problem-stale-data-after-reloading">The Problem: Stale Data After Reloading</h2>

<p>Before this update, calling <a href="https://apidock.com/rails/ActiveRecord/Base/reload">reload</a>! refreshed your classes and constants, but it didn’t necessarily “reset” the current Executor.</p>

<p>This led to a frustrating edge case. Since the Executor enables the <strong>Query Cache</strong> by default, you could run a query, call <code class="language-ruby highlighter-rouge"><span class="n">reload!</span></code>, and run the same query again, only to see the <em>old</em> results because the cache hadn’t been cleared. The code was new, but the data was stale.</p>

<h2 id="the-solution-a-deeper-reset">The Solution: A Deeper Reset</h2>

<p>The implementation ensures that <code class="language-ruby highlighter-rouge"><span class="n">reload!</span></code> now triggers a full reset of the Rails Executor.</p>

<p>Now, when you run the command:</p>

<ol>
  <li><strong>Constants are cleared:</strong> Zeitwerk reloads your modified models, controllers, and lib files.</li>
  <li><strong>The Executor is reset:</strong> This forces the Query Cache to be purged and ensures all internal “per-request” states are wiped clean.</li>
</ol>

<h3 id="what-it-looks-like-in-practice">What it looks like in practice:</h3>

<div class="language-irb highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="rouge-code"><pre><span class="c"># Start your console
</span><span class="go">user = User.first 
</span><span class="c"># (SQL: SELECT * FROM users LIMIT 1)
</span><span class="err">
</span><span class="c"># Imagine a background job or another process updates that user in the DB
# ...
</span><span class="err">
</span><span class="go">reload! 
</span><span class="c"># Now triggers a full Executor reset via PR #56639
</span><span class="err">
</span><span class="go">User.first 
</span><span class="c"># (SQL: SELECT * FROM users LIMIT 1) 
# Guaranteed to fetch fresh data from the database
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>This change is all about reducing surprises. By making <code class="language-ruby highlighter-rouge"><span class="n">reload!</span></code> reset the Executor, the console now perfectly mirrors the “clean slate” that a user gets on a fresh browser request. It eliminates those “ghost bugs” where you think your code isn’t working, only to realize later that you were looking at cached database results from earlier in the session.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[For any Rails developer, the reload! command is a lifeline. Whether you’re testing a new method or debugging a complex query, the ability to refresh the application code without restarting the console session is a massive productivity boost.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/rails-console-making-reload-truly-reliable.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/rails-console-making-reload-truly-reliable.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Ruby Internals: The Spaceship Operator</title><link href="https://www.mintbit.com/blog/ruby-internals-the-spaceship-operator/" rel="alternate" type="text/html" title="Ruby Internals: The Spaceship Operator" /><published>2026-01-26T08:17:00-05:00</published><updated>2026-01-26T08:17:00-05:00</updated><id>https://www.mintbit.com/blog/ruby-internals-the-spaceship-operator</id><content type="html" xml:base="https://www.mintbit.com/blog/ruby-internals-the-spaceship-operator/"><![CDATA[<p>At the heart of Ruby’s comparison and sorting mechanics lies the <code class="language-ruby highlighter-rouge"><span class="o">&lt;=&gt;</span></code> operator, popularly known as the <strong>Spaceship Operator</strong>. It is the fundamental building block that allows the language to determine the order of magnitude between objects, serving as the engine behind ubiquitous methods like <a href="https://apidock.com/ruby/Array/sort">sort</a> and <a href="https://apidock.com/ruby/Array/min">min</a>.</p>

<h2 id="what-is-the-spaceship-operator">What is the Spaceship Operator?</h2>

<p>The <code class="language-ruby highlighter-rouge"><span class="o">&lt;=&gt;</span></code> operator is a trivalent comparison method. Unlike traditional operators (<code class="language-ruby highlighter-rouge"><span class="o">&lt;</span></code> or <code class="language-ruby highlighter-rouge"><span class="o">&gt;</span></code>) that return a boolean value, the spaceship returns an <code class="language-ruby highlighter-rouge"><span class="no">Integer</span></code> (or <code class="language-ruby highlighter-rouge"><span class="kp">nil</span></code> if the objects are not comparable):</p>

<ul>
  <li><strong>-1</strong>: The left object is <strong>less than</strong> the right object.</li>
  <li><strong>0</strong>: The objects are <strong>equal</strong>.</li>
  <li><strong>1</strong>: The left object is <strong>greater than</strong> the right object.</li>
</ul>

<h2 id="array-comparison-lexicographical-order">Array Comparison: Lexicographical Order</h2>

<p>When applied to Arrays, Ruby utilizes an element-by-element comparison logic known as lexicographical order (following the same strategy as a dictionary).</p>

<p>The interpreter traverses each index comparing the values:</p>

<ol>
  <li>The search stops at the first pair of elements that are not equal.</li>
  <li>The result of comparing that specific pair determines the final result for the entire Array.</li>
  <li>If all elements are equal until the end of one Array, the shorter Array is considered “less than.”</li>
</ol>

<h3 id="execution-examples">Execution Examples</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="o">&lt;=&gt;</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span> <span class="c1"># =&gt; -1 (comparison stops at index 2: 3 &lt; 4)</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">&lt;=&gt;</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span> <span class="c1"># =&gt; 1  (comparison stops at index 1: 5 &gt; 2)</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>    <span class="o">&lt;=&gt;</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="c1"># =&gt; -1 (the left array ends first)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="the-role-of-enumerable-and-comparable-modules">The Role of Enumerable and Comparable Modules</h2>

<p>The true power of <code class="language-ruby highlighter-rouge"><span class="o">&lt;=&gt;</span></code> lies in its integration with the Ruby core. The <code class="language-ruby highlighter-rouge"><span class="no">Enumerable</span><span class="c1">#sort</span></code> method, essential in any Rails application, uses the spaceship operator internally to determine the position of each element in the collection.</p>

<p>By implementing the <code class="language-ruby highlighter-rouge"><span class="o">&lt;=&gt;</span></code> method in a custom class and including the <code class="language-ruby highlighter-rouge"><span class="no">Comparable</span></code> module, the object automatically gains support for all logical operators: <code class="language-ruby highlighter-rouge"><span class="o">&lt;</span></code>, <code class="language-ruby highlighter-rouge"><span class="o">&gt;</span></code>, <code class="language-ruby highlighter-rouge"><span class="o">&lt;=</span></code>, <code class="language-ruby highlighter-rouge"><span class="o">&gt;=</span></code>, <code class="language-ruby highlighter-rouge"><span class="o">==</span></code>, and the <code class="language-ruby highlighter-rouge"><span class="n">between?</span></code> method.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Product</span>
  <span class="kp">include</span> <span class="no">Comparable</span>
  <span class="nb">attr_reader</span> <span class="ss">:price</span>

  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">price</span><span class="p">)</span>
    <span class="vi">@price</span> <span class="o">=</span> <span class="n">price</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">&lt;=&gt;</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
    <span class="k">return</span> <span class="kp">nil</span> <span class="k">unless</span> <span class="n">other</span><span class="p">.</span><span class="nf">is_a?</span><span class="p">(</span><span class="no">Product</span><span class="p">)</span>
    <span class="n">price</span> <span class="o">&lt;=&gt;</span> <span class="n">other</span><span class="p">.</span><span class="nf">price</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="application-in-rails-development">Application in Rails Development</h2>

<p>Understanding the mechanics of the spaceship operator is crucial for Rails developers when dealing with:</p>

<ul>
  <li><strong>In-memory sorting:</strong> When it is necessary to sort <code class="language-ruby highlighter-rouge"><span class="no">ActiveRecord</span></code> collections after complex Ruby-side manipulations.</li>
  <li><strong>Value Objects:</strong> Creating domain objects representing measurements or prices that require range validations.</li>
  <li><strong>Performance:</strong> Knowing that the comparison of long Arrays terminates as soon as the first difference is found helps predict the computational cost of certain sorting operations.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[At the heart of Ruby’s comparison and sorting mechanics lies the &lt;=&gt; operator, popularly known as the Spaceship Operator. It is the fundamental building block that allows the language to determine the order of magnitude between objects, serving as the engine behind ubiquitous methods like sort and min.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/ruby-internals-the-spaceship-operator.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/ruby-internals-the-spaceship-operator.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Native SVG Rendering Support</title><link href="https://www.mintbit.com/blog/native-svg-rendering-support/" rel="alternate" type="text/html" title="Native SVG Rendering Support" /><published>2026-01-26T06:40:00-05:00</published><updated>2026-01-26T06:40:00-05:00</updated><id>https://www.mintbit.com/blog/native-svg-rendering-support</id><content type="html" xml:base="https://www.mintbit.com/blog/native-svg-rendering-support/"><![CDATA[<p>For years, Rails has provided seamless ways to respond with various data formats. Yet, SVG—despite being raw XML—often required manual workarounds. Developers typically had to manually set the <code class="language-ruby highlighter-rouge"><span class="n">image</span><span class="o">/</span><span class="n">svg</span><span class="o">+</span><span class="n">xml</span></code> MIME type or create separate <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">erb</span></code> templates just to wrap a string.</p>

<p>The introduction of a dedicated SVG renderer eliminates this friction. By recognizing <code class="language-ruby highlighter-rouge"><span class="ss">:svg</span></code> as a first-class format, Rails treats vector graphics with the same level of priority and automation as a standard JSON response.</p>

<h2 id="the-to_svg-convention">The to_svg Convention</h2>

<p>The core of this update is the reliance on duck typing. If an object responds to <code class="language-ruby highlighter-rouge"><span class="n">to_svg</span></code>, Rails knows exactly how to handle the delivery. This shifts the responsibility of the graphical representation to the object itself, ensuring it knows how to visualize its own data.</p>

<h3 id="1-implementation-in-the-model">1. Implementation in the Model</h3>

<p>The logic for generating the XML stays encapsulated. This makes it easy to test and reuse across different parts of the application.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Page</span>
  <span class="k">def</span> <span class="nf">to_svg</span>
    <span class="c1"># This method returns the raw XML string </span>
    <span class="c1"># generated by a library or custom logic</span>
    <span class="n">qr_code</span> 
  <span class="k">end</span>
<span class="k">end</span>

</pre></td></tr></tbody></table></code></pre></div></div>

<h3 id="2-elegant-controller-integration">2. Elegant Controller Integration</h3>

<p>In the controller, the implementation is reduced to a single line within the <code class="language-ruby highlighter-rouge"><span class="n">respond_to</span></code> block. The framework handles the header assignment and method invocation automatically.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">PagesController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
  <span class="k">def</span> <span class="nf">show</span>
    <span class="vi">@page</span> <span class="o">=</span> <span class="no">Page</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="ss">:id</span><span class="p">])</span>

    <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
      <span class="nb">format</span><span class="p">.</span><span class="nf">html</span>
      <span class="nb">format</span><span class="p">.</span><span class="nf">svg</span> <span class="p">{</span> <span class="n">render</span> <span class="ss">svg: </span><span class="vi">@page</span> <span class="p">}</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>

</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="technical-architecture-and-integration">Technical Architecture and Integration</h2>

<p>This approach reinforces the “Skinny Controller” philosophy. By delegating the XML generation to the model layer, the controller focuses solely on request handling and format negotiation.</p>

<p>Because the renderer is built directly into the Rails core, it ensures that the <code class="language-ruby highlighter-rouge"><span class="no">Content</span><span class="o">-</span><span class="no">Type</span></code> header is always strictly compliant with browser standards for vector images. This prevents common bugs where SVGs are treated as plain text or downloaded instead of rendered inline.</p>

<p>The native SVG renderer is a classic example of Rails identifying a recurring pattern and baking it into the framework. It streamlines the delivery of visual data and ensures that the codebase remains expressive and aligned with established conventions. By reducing boilerplate and promoting encapsulation, it helps maintain high standards of code quality.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[For years, Rails has provided seamless ways to respond with various data formats. Yet, SVG—despite being raw XML—often required manual workarounds. Developers typically had to manually set the image/svg+xml MIME type or create separate .erb templates just to wrap a string.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/native-svg-rendering-support.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/native-svg-rendering-support.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Ruby Internals: The Method Lookup Path</title><link href="https://www.mintbit.com/blog/ruby-internals-the-method-lookup-path/" rel="alternate" type="text/html" title="Ruby Internals: The Method Lookup Path" /><published>2026-01-25T14:14:00-05:00</published><updated>2026-01-25T14:14:00-05:00</updated><id>https://www.mintbit.com/blog/ruby-internals-the-method-lookup-path</id><content type="html" xml:base="https://www.mintbit.com/blog/ruby-internals-the-method-lookup-path/"><![CDATA[<p>Understanding how Ruby finds a method—known as <strong>Method Lookup</strong>—is the boundary between writing code and truly mastering the language. In a Rails environment, where gems, concerns, and inheritance are constantly interacting, knowing the <strong>Ancestors Chain</strong> is essential for debugging and designing robust architectures.</p>

<h2 id="the-ancestors-chain-rubys-search-algorithm">The Ancestors Chain: Ruby’s Search Algorithm</h2>

<p>When you call a method on a Ruby object, the interpreter doesn’t look everywhere at once. It follows a strictly linear, vertical path. It starts at the most specific point (the object’s instance) and moves up until it finds the first implementation of the method.</p>

<p>The search order follows this hierarchy:</p>

<ol>
  <li><strong>Singleton Class (Eigenclass):</strong> Methods defined strictly for that specific instance (e.g., <code class="language-ruby highlighter-rouge"><span class="k">def</span> <span class="nc">user</span><span class="o">.</span><span class="nf">admin?</span></code>).</li>
  <li><strong>Prepended Modules:</strong> Modules added via <code class="language-ruby highlighter-rouge"><span class="n">prepend</span></code> sit “in front” of the class.</li>
  <li><strong>The Class:</strong> The actual class where the object was instantiated.</li>
  <li><strong>Included Modules:</strong> Modules added via <code class="language-ruby highlighter-rouge"><span class="kp">include</span></code>. If multiple modules are included, they are searched in reverse order of declaration (the last one included is searched first).</li>
  <li><strong>Superclass:</strong> The parent class (where the entire logic repeats).</li>
  <li><strong>Object / Kernel / BasicObject:</strong> The root of nearly all Ruby objects.</li>
</ol>

<h2 id="visualizing-the-path">Visualizing the Path</h2>

<p>You can inspect this path in any Rails console or Ruby script using the <a href="https://apidock.com/ruby/Module/ancestors">ancestors</a> method. It returns an array representing the exact lookup order.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="k">module</span> <span class="nn">Authenticatable</span>
  <span class="k">def</span> <span class="nf">login</span><span class="p">;</span> <span class="s2">"Logged in!"</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>

<span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">Authenticatable</span>
<span class="k">end</span>

<span class="nb">puts</span> <span class="no">User</span><span class="p">.</span><span class="nf">ancestors</span>
<span class="c1"># =&gt; [User, Authenticatable, ActiveRecord::Base, ..., Object, Kernel, BasicObject]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="the-wrapper-pattern-prepend-vs-include">The “Wrapper” Pattern: Prepend vs. Include</h2>

<p>The choice between <a href="https://apidock.com/ruby/Module/include">include</a> and <a href="https://apidock.com/ruby/Module/prepend">prepend</a> is often misunderstood. It is entirely about where the module is placed in the lookup path:</p>

<ul>
  <li><strong><code class="language-ruby highlighter-rouge"><span class="kp">include</span></code>:</strong> The module is placed <strong>immediately after</strong> the class. If both define the same method, the class’s version wins.</li>
  <li><strong><code class="language-ruby highlighter-rouge"><span class="n">prepend</span></code>:</strong> The module is placed <strong>before</strong> the class. This allows the module to override the class method and optionally call <code class="language-ruby highlighter-rouge"><span class="k">super</span></code> to trigger the class’s original logic.</li>
</ul>

<p>This makes <code class="language-ruby highlighter-rouge"><span class="n">prepend</span></code> the ideal tool for the “Decorator” or “Wrapper” pattern, common in performance monitoring or logging gems.</p>

<h2 id="handling-failure-method_missing">Handling Failure: method_missing</h2>

<p>What happens if Ruby reaches <code class="language-ruby highlighter-rouge"><span class="no">BasicObject</span></code> and still hasn’t found the method? It doesn’t give up immediately. It starts a <strong>second search</strong> from the beginning of the chain, this time looking for a method called <a href="https://apidock.com/ruby/BasicObject/method_missing">method_missing</a>.</p>

<p>This is where the “magic” of Rails happens. Features like dynamic finders (e.g., <code class="language-ruby highlighter-rouge"><span class="no">User</span><span class="p">.</span><span class="nf">find_by_email</span></code>) or OpenStruct attributes rely on catching a failed lookup and handling it dynamically. If <code class="language-ruby highlighter-rouge"><span class="nb">method_missing</span></code> is also not found (or if it calls <code class="language-ruby highlighter-rouge"><span class="k">super</span></code>), Ruby finally raises the <code class="language-ruby highlighter-rouge"><span class="no">NoMethodError</span></code>.</p>

<h2 id="why-this-matters-for-rails-developers">Why This Matters for Rails Developers</h2>

<ol>
  <li><strong>Debugging Conflict:</strong> If two gems or concerns define a method with the same name, the one higher in the <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">ancestors</span></code> list will silently “hide” the other.</li>
  <li><strong>Super Calls:</strong> Understanding the chain is the only way to know exactly which method <code class="language-ruby highlighter-rouge"><span class="k">super</span></code> will trigger. It isn’t always the parent class; it could be an included module.</li>
  <li><strong>Performance:</strong> While Ruby’s method caching is highly optimized, an excessively deep ancestors chain (common in “over-architected” systems) can lead to subtle performance overhead.</li>
</ol>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[Understanding how Ruby finds a method—known as Method Lookup—is the boundary between writing code and truly mastering the language. In a Rails environment, where gems, concerns, and inheritance are constantly interacting, knowing the Ancestors Chain is essential for debugging and designing robust architectures.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/ruby-internals-the-method-lookup-path.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/ruby-internals-the-method-lookup-path.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ActiveRecord: only_columns</title><link href="https://www.mintbit.com/blog/activerecord-only-columns/" rel="alternate" type="text/html" title="ActiveRecord: only_columns" /><published>2026-01-16T12:03:00-05:00</published><updated>2026-01-16T12:03:00-05:00</updated><id>https://www.mintbit.com/blog/activerecord-only-columns</id><content type="html" xml:base="https://www.mintbit.com/blog/activerecord-only-columns/"><![CDATA[<p>In Rails, we’ve always had <a href="https://apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/ignored_columns">ignored_columns</a> to tell ActiveRecord: “Ignore these columns, even if they exist in the database.” But what if we want the opposite? What if we have a table with dozens of columns and our specific service only needs three?</p>

<p>The <code class="language-ruby highlighter-rouge"><span class="n">only_columns</span></code> method allows for an “allowlist” approach to model attributes, giving you precise control over what data your application interacts with.</p>

<h2 id="inverting-the-logic">Inverting the Logic</h2>

<p>There are two main scenarios where listing ignored columns becomes tedious:</p>

<ol>
  <li><strong>Shared Databases:</strong> When multiple applications share the same database, but a specific service should only “know” about its own relevant fields.</li>
  <li><strong>Column Deprecation:</strong> The safe process of deleting a column usually involves ignoring it across all services first. If you have multiple microservices, you’d have to update every single one.</li>
</ol>

<p>With <code class="language-ruby highlighter-rouge"><span class="n">only_columns</span></code>, you define a strict contract. Instead of listing what you <strong>don’t</strong> want, you list exactly what the model is authorized to see. Anything not on this list is automatically ignored by ActiveRecord.</p>

<h3 id="example">Example:</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="c1"># The model will only recognize these 3 columns, </span>
  <span class="c1"># ignoring everything else in the table.</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">only_columns</span> <span class="o">=</span> <span class="sx">%w[id email created_at]</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>If a new <code class="language-ruby highlighter-rouge"><span class="n">secret_token</span></code> column is added to the <code class="language-ruby highlighter-rouge"><span class="n">users</span></code> table, this specific service won’t even realize it exists, ensuring data isolation and security.</p>

<h2 id="a-more-scalable-workflow">A More Scalable Workflow</h2>

<p>This feature is a major win for maintenance. If your service only uses 5 columns out of a 100-column table, you never have to update an “ignore list” again when other teams modify the schema. It provides security by omission, keeps objects in memory lighter, and makes your data contract explicit.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[In Rails, we’ve always had ignored_columns to tell ActiveRecord: “Ignore these columns, even if they exist in the database.” But what if we want the opposite? What if we have a table with dozens of columns and our specific service only needs three?]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/activerecord-only-columns.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/activerecord-only-columns.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">The rename_schema Migration Helper</title><link href="https://www.mintbit.com/blog/the-rename-schema-migration-helper/" rel="alternate" type="text/html" title="The rename_schema Migration Helper" /><published>2026-01-13T11:49:00-05:00</published><updated>2026-01-13T11:49:00-05:00</updated><id>https://www.mintbit.com/blog/the-rename-schema-migration-helper</id><content type="html" xml:base="https://www.mintbit.com/blog/the-rename-schema-migration-helper/"><![CDATA[<p>Schemas are essential for organizing tables or managing multi-tenant architectures. While Rails has long supported basic database operations, renaming a schema used to require dropping down into raw SQL. Since Rails 7.0, the <a href="https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQL/SchemaStatements.html#method-i-rename_schema">rename_schema</a> method has brought this capability directly into the ActiveRecord migration DSL for those using PostgreSQL.</p>

<h2 id="beyond-raw-sql">Beyond raw SQL</h2>

<p>In the past, when a business pivot or architectural change required a schema name update (e.g., from <code class="language-ruby highlighter-rouge"><span class="n">v1_internal</span></code> to <code class="language-ruby highlighter-rouge"><span class="n">legacy_data</span></code>), you had to rely on strings and manual execution:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">up</span>
  <span class="n">execute</span> <span class="s2">"ALTER SCHEMA v1_internal RENAME TO legacy_data"</span>
<span class="k">end</span>

</pre></td></tr></tbody></table></code></pre></div></div>

<p>The problem with this approach is that it’s not naturally reversible, forcing you to write a separate <code class="language-ruby highlighter-rouge"><span class="n">down</span></code> method to handle rollbacks.</p>

<h2 id="clean-and-reversible-migrations">Clean and Reversible Migrations</h2>

<p>With <code class="language-ruby highlighter-rouge"><span class="n">rename_schema</span></code>, the operation becomes declarative. Rails handles the PostgreSQL heavy lifting, and more importantly, it makes the migration automatically reversible.</p>

<h3 id="how-it-looks">How it looks:</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">RenameInternalSchema</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span><span class="p">[</span><span class="mf">7.0</span><span class="p">]</span>
  <span class="k">def</span> <span class="nf">change</span>
    <span class="n">rename_schema</span> <span class="ss">:v1_internal</span><span class="p">,</span> <span class="ss">:private_data</span>
  <span class="k">end</span>
<span class="k">end</span>

</pre></td></tr></tbody></table></code></pre></div></div>

<p>Using a native helper instead of raw SQL strings keeps your migrations consistent with the rest of your codebase. It’s particularly useful for enterprise applications where schemas are used to isolate client data or version different parts of the database. By staying within the DSL, you ensure that your schema history remains clean and that rollbacks are as simple as running <code class="language-ruby highlighter-rouge"><span class="n">bin</span><span class="o">/</span><span class="n">rails</span> <span class="n">db</span><span class="ss">:rollback</span></code>.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[Schemas are essential for organizing tables or managing multi-tenant architectures. While Rails has long supported basic database operations, renaming a schema used to require dropping down into raw SQL. Since Rails 7.0, the rename_schema method has brought this capability directly into the ActiveRecord migration DSL for those using PostgreSQL.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/the-rename-schema-migration-helper.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/the-rename-schema-migration-helper.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Update_columns with touch</title><link href="https://www.mintbit.com/blog/update-columns-with-touch/" rel="alternate" type="text/html" title="Update_columns with touch" /><published>2026-01-13T11:30:00-05:00</published><updated>2026-01-13T11:30:00-05:00</updated><id>https://www.mintbit.com/blog/update-columns-with-touch</id><content type="html" xml:base="https://www.mintbit.com/blog/update-columns-with-touch/"><![CDATA[<p>The methods <a href="https://apidock.com/rails/v7.1.3.4/ActiveRecord/Persistence/update_columns">update_columns</a> and <a href="https://apidock.com/rails/v7.1.3.4/ActiveRecord/Persistence/update_column">update_column</a> are well-known for their speed because they bypass all Rails validations and callbacks. However, they had a major “side effect”: by default, they wouldn’t update the <code class="language-ruby highlighter-rouge"><span class="n">updated_at</span></code> timestamp.</p>

<p><a href="https://github.com/rails/rails/pull/51455">PR #51455</a>, merged in July 2025, solves this dilemma by allowing you to update specific columns and the timestamp in one go. Even though this was introduced a few months ago, it’s a game-changer worth remembering for any performance-tuned Rails app.</p>

<h2 id="the-problem-performance-vs-timestamps">The Problem: Performance vs. Timestamps</h2>

<p>Whenever we used <code class="language-ruby highlighter-rouge"><span class="c1">#update_columns</span></code> to gain performance (avoiding expensive callbacks), we lost the information of when the record was actually modified—unless we manually updated the <code class="language-ruby highlighter-rouge"><span class="n">updated_at</span></code> field:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="c1"># Before: You had to update the timestamp manually</span>
<span class="n">user</span><span class="p">.</span><span class="nf">update_columns</span><span class="p">(</span><span class="ss">last_login_at: </span><span class="no">Time</span><span class="p">.</span><span class="nf">current</span><span class="p">,</span> <span class="ss">updated_at: </span><span class="no">Time</span><span class="p">.</span><span class="nf">current</span><span class="p">)</span>

<span class="c1"># Or run two separate database commands</span>
<span class="n">user</span><span class="p">.</span><span class="nf">update_column</span><span class="p">(</span><span class="ss">:last_login_at</span><span class="p">,</span> <span class="no">Time</span><span class="p">.</span><span class="nf">current</span><span class="p">)</span>
<span class="n">user</span><span class="p">.</span><span class="nf">touch</span>

</pre></td></tr></tbody></table></code></pre></div></div>

<p>This led to repetitive code and was prone to bugs, especially for ETL processes or cache keys that rely heavily on the <code class="language-ruby highlighter-rouge"><span class="n">updated_at</span></code> field.</p>

<h2 id="the-solution-the-new-touch-option">The Solution: The new :touch option</h2>

<p>Now, you can simply pass <code class="language-ruby highlighter-rouge"><span class="ss">touch: </span><span class="kp">true</span></code> to ensure that Rails’ audit timestamps are updated within the same SQL query.</p>

<h3 id="practical-example">Practical Example:</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="c1"># Updates the column and updated_at in a single, efficient query</span>
<span class="n">user</span><span class="p">.</span><span class="nf">update_columns</span><span class="p">(</span><span class="ss">active: </span><span class="kp">true</span><span class="p">,</span> <span class="ss">touch: </span><span class="kp">true</span><span class="p">)</span>

<span class="c1"># It also works for a single column update</span>
<span class="n">user</span><span class="p">.</span><span class="nf">update_column</span><span class="p">(</span><span class="ss">:active</span><span class="p">,</span> <span class="kp">true</span><span class="p">,</span> <span class="ss">touch: </span><span class="kp">true</span><span class="p">)</span>

</pre></td></tr></tbody></table></code></pre></div></div>

<p>You can even pass a specific time if needed:
<code class="language-ruby highlighter-rouge"><span class="n">user</span><span class="p">.</span><span class="nf">update_columns</span><span class="p">(</span><span class="ss">active: </span><span class="kp">true</span><span class="p">,</span> <span class="ss">touch: </span><span class="p">{</span> <span class="ss">time: </span><span class="mi">1</span><span class="p">.</span><span class="nf">day</span><span class="p">.</span><span class="nf">ago</span> <span class="p">})</span></code></p>

<h2 id="why-it-matters">Why It Matters</h2>

<ul>
  <li><strong>Performance with Integrity:</strong> You keep the speed of bypassing callbacks without “breaking” the application’s timestamp logic.</li>
  <li><strong>Single Query:</strong> Reduces database round-trips, as you no longer need a separate <code class="language-ruby highlighter-rouge"><span class="n">touch</span></code> command.</li>
  <li><strong>Clean Code:</strong> Removes the need to explicitly include <code class="language-ruby highlighter-rouge"><span class="ss">updated_at: </span><span class="no">Time</span><span class="p">.</span><span class="nf">current</span></code> in every direct update call.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[The methods update_columns and update_column are well-known for their speed because they bypass all Rails validations and callbacks. However, they had a major “side effect”: by default, they wouldn’t update the updated_at timestamp.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/update-columns-with-touch.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/update-columns-with-touch.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ActiveRecord: Improving Readability with Array#inquiry</title><link href="https://www.mintbit.com/blog/improving-readability-with-array-inquiry/" rel="alternate" type="text/html" title="ActiveRecord: Improving Readability with Array#inquiry" /><published>2026-01-09T07:59:00-05:00</published><updated>2026-01-09T07:59:00-05:00</updated><id>https://www.mintbit.com/blog/improving-readability-with-array-inquiry</id><content type="html" xml:base="https://www.mintbit.com/blog/improving-readability-with-array-inquiry/"><![CDATA[<p>In Rails, we are used to using the <a href="https://apidock.com/rails/v7.1.3.2/String/inquiry">.inquiry</a> method on strings (like the famous <code class="language-ruby highlighter-rouge"><span class="no">Rails</span><span class="p">.</span><span class="nf">env</span><span class="p">.</span><span class="nf">development?</span></code>). However, ActiveSupport also extends this functionality to Arrays, transforming a simple list into an object capable of answering questions semantically.</p>

<p><code class="language-ruby highlighter-rouge"><span class="no">Array</span><span class="c1">#inquiry</span></code> is perfect for when you have a list of options and want to check for the presence of an item without using the traditional <a href="https://apidock.com/ruby/Array/include%3F">.include</a>?.</p>

<h2 id="the-problem-repetitive-and-unreadable-checks">The Problem: Repetitive and Unreadable Checks</h2>

<p>Imagine you have an array of roles for a user. The standard way to check a permission would be:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="n">roles</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"admin"</span><span class="p">,</span> <span class="s2">"editor"</span><span class="p">]</span>

<span class="k">if</span> <span class="n">roles</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="s2">"admin"</span><span class="p">)</span>
  <span class="c1"># do something</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>It works, but it’s not very “Rails Way.” The code gets cluttered with parentheses and strings, losing that fluid readability that Ruby is known for.</p>

<h2 id="the-solution-turning-the-array-into-an-inquirer">The Solution: Turning the Array into an Inquirer</h2>

<p>By calling <code class="language-ruby highlighter-rouge"><span class="p">.</span><span class="nf">inquiry</span></code> on an array, Rails wraps that list in an <code class="language-ruby highlighter-rouge"><span class="no">ActiveSupport</span><span class="o">::</span><span class="no">ArrayInquirer</span></code> object. This allows you to ask questions using methods ending in <code class="language-ruby highlighter-rouge"><span class="p">?</span></code>.</p>

<h3 id="practical-example">Practical Example:</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="n">roles</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"admin"</span><span class="p">,</span> <span class="s2">"editor"</span><span class="p">].</span><span class="nf">inquiry</span>

<span class="n">roles</span><span class="p">.</span><span class="nf">admin?</span>  <span class="c1"># =&gt; true</span>
<span class="n">roles</span><span class="p">.</span><span class="nf">editor?</span> <span class="c1"># =&gt; true</span>
<span class="n">roles</span><span class="p">.</span><span class="nf">guest?</span>  <span class="c1"># =&gt; false</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>You can also check for multiple items at once by passing arguments:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">roles</span><span class="p">.</span><span class="nf">any?</span><span class="p">(</span><span class="ss">:admin</span><span class="p">,</span> <span class="ss">:moderator</span><span class="p">)</span> <span class="c1"># =&gt; true</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-it-matters">Why It Matters</h2>

<ul>
  <li><strong>Readability:</strong> <code class="language-ruby highlighter-rouge"><span class="n">roles</span><span class="p">.</span><span class="nf">admin?</span></code> is much more natural for a human to read than <code class="language-ruby highlighter-rouge"><span class="n">roles</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="s2">"admin"</span><span class="p">)</span></code>.</li>
  <li><strong>Fewer Errors:</strong> Since you are using method calls, the code is visually cleaner, making it easier to identify permission logic.</li>
  <li><strong>Standardization:</strong> It follows the same pattern we already use for <code class="language-ruby highlighter-rouge"><span class="no">Rails</span><span class="p">.</span><span class="nf">env</span></code> or ActiveRecord Enums.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[In Rails, we are used to using the .inquiry method on strings (like the famous Rails.env.development?). However, ActiveSupport also extends this functionality to Arrays, transforming a simple list into an object capable of answering questions semantically.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/improving-readability-with-array-inquiry.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/improving-readability-with-array-inquiry.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Beyond db:seed: How to Use db:seed:replant in Rails</title><link href="https://www.mintbit.com/blog/beyond-db-seed-how-to-use-db-seed-replant-in-rails/" rel="alternate" type="text/html" title="Beyond db:seed: How to Use db:seed:replant in Rails" /><published>2026-01-09T07:12:00-05:00</published><updated>2026-01-09T07:12:00-05:00</updated><id>https://www.mintbit.com/blog/beyond-db-seed-how-to-use-db-seed-replant-in-rails</id><content type="html" xml:base="https://www.mintbit.com/blog/beyond-db-seed-how-to-use-db-seed-replant-in-rails/"><![CDATA[<p>Every Rails developer is familiar with <code class="language-ruby highlighter-rouge"><span class="n">rails</span> <span class="n">db</span><span class="ss">:seed</span></code>, the command used to populate the database with initial data. But what happens when you modify your <code class="language-ruby highlighter-rouge"><span class="n">seeds</span><span class="p">.</span><span class="nf">rb</span></code> file and want to start fresh without deleting the entire database? That’s where <code class="language-ruby highlighter-rouge"><span class="n">rails</span> <span class="n">db</span><span class="ss">:seed:replant</span></code> comes in.</p>

<p>This command is a productive shortcut for anyone who needs to “clean up and start over” with the application’s core data.</p>

<h2 id="the-problem-duplicate-data-and-database-clutter">The Problem: Duplicate Data and Database Clutter</h2>

<p>If you run the <code class="language-ruby highlighter-rouge"><span class="n">rails</span> <span class="n">db</span><span class="ss">:seed</span></code> command multiple times, Rails simply executes the script again. If your script isn’t smart enough to check if a record already exists (using <code class="language-ruby highlighter-rouge"><span class="n">find_or_create_by</span></code>, for example), you’ll end up with duplicate data.</p>

<p>The old-school solution was to run <code class="language-ruby highlighter-rouge"><span class="n">rails</span> <span class="n">db</span><span class="ss">:migrate:reset</span> <span class="n">db</span><span class="ss">:seed</span></code>, but this is slow because it drops the database, recreates the tables, and runs all migrations from scratch.</p>

<h2 id="the-solution-clear-without-destroying">The Solution: Clear Without Destroying</h2>

<p>The <code class="language-ruby highlighter-rouge"><span class="n">rails</span> <span class="n">db</span><span class="ss">:seed:replant</span></code> command performs two actions atomically:</p>

<ol>
  <li><strong>Truncate:</strong> It empties all tables in your database (clearing the data while keeping the table structure intact).</li>
  <li><strong>Seed:</strong> It executes the <code class="language-ruby highlighter-rouge"><span class="n">db</span><span class="o">/</span><span class="n">seeds</span><span class="p">.</span><span class="nf">rb</span></code> script.</li>
</ol>

<p>It is significantly faster than a full reset because it doesn’t touch the schema; it only affects the content.</p>

<h3 id="how-to-use-it">How to use it:</h3>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>rails db:seed:replant
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-it-matters">Why It Matters</h2>

<ul>
  <li><strong>Speed:</strong> It’s the fastest way to reset your data state during development.</li>
  <li><strong>Schema Integrity:</strong> You don’t risk running into issues with pending migrations, as the structure remains untouched.</li>
  <li><strong>Convenience:</strong> A single command replaces the need to manually delete records or reset the entire database.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[Every Rails developer is familiar with rails db:seed, the command used to populate the database with initial data. But what happens when you modify your seeds.rb file and want to start fresh without deleting the entire database? That’s where rails db:seed:replant comes in.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/beyond-db-seed-how-to-use-db-seed-replant-in-rails.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/beyond-db-seed-how-to-use-db-seed-replant-in-rails.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Understanding extend self</title><link href="https://www.mintbit.com/blog/understanding-extend-self/" rel="alternate" type="text/html" title="Understanding extend self" /><published>2026-01-07T12:54:00-05:00</published><updated>2026-01-07T12:54:00-05:00</updated><id>https://www.mintbit.com/blog/understanding-extend-self</id><content type="html" xml:base="https://www.mintbit.com/blog/understanding-extend-self/"><![CDATA[<p>In Ruby, we often create modules to serve as libraries of utility functions. You’ve likely seen modules in various projects that use <code class="language-ruby highlighter-rouge"><span class="kp">extend</span> <span class="nb">self</span></code> right at the top. But what does it actually do, and why not just use <code class="language-ruby highlighter-rouge"><span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">method</span></code>?</p>

<p><code class="language-ruby highlighter-rouge"><span class="kp">extend</span> <span class="nb">self</span></code> is an idiomatic way to make a module’s methods available both as instance methods and as module methods.</p>

<h2 id="the-problem-repetition-and-rigidity">The Problem: Repetition and Rigidity</h2>

<p>When creating a utility module, you usually want to call its methods directly:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">module</span> <span class="nn">Calculator</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
    <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="no">Calculator</span><span class="p">.</span><span class="nf">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># Works</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>The issue is that if you have 10 methods, you have to write <code class="language-ruby highlighter-rouge"><span class="nb">self</span><span class="o">.</span></code> for every single one. Furthermore, if you want to <code class="language-ruby highlighter-rouge"><span class="kp">include</span></code> this module in a class to use those methods internally, <code class="language-ruby highlighter-rouge"><span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">add</span></code> won’t be available as an instance method for that class.</p>

<h2 id="the-solution-the-extend-self-shortcut">The Solution: The extend self Shortcut</h2>

<p>By using <code class="language-ruby highlighter-rouge"><span class="kp">extend</span> <span class="nb">self</span></code>, you are telling Ruby: “Take all the instance methods defined here and add them to the module itself as well.”</p>

<h3 id="practical-example">Practical Example:</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="rouge-code"><pre><span class="k">module</span> <span class="nn">Formatter</span>
  <span class="kp">extend</span> <span class="nb">self</span>

  <span class="k">def</span> <span class="nf">capitalize_name</span><span class="p">(</span><span class="nb">name</span><span class="p">)</span>
    <span class="nb">name</span><span class="p">.</span><span class="nf">capitalize</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="c1"># Usage 1: Direct module call</span>
<span class="no">Formatter</span><span class="p">.</span><span class="nf">capitalize_name</span><span class="p">(</span><span class="s2">"gemini"</span><span class="p">)</span> <span class="c1"># =&gt; "Gemini"</span>

<span class="c1"># Usage 2: Including in a class</span>
<span class="k">class</span> <span class="nc">User</span>
  <span class="kp">include</span> <span class="no">Formatter</span>
  
  <span class="k">def</span> <span class="nf">display_name</span>
    <span class="n">capitalize_name</span><span class="p">(</span><span class="s2">"alex"</span><span class="p">)</span> <span class="c1"># Works here too!</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-it-matters">Why It Matters</h2>

<ul>
  <li><strong>DRY (Don’t Repeat Yourself):</strong> You write the method name only once, without the <code class="language-ruby highlighter-rouge"><span class="nb">self</span><span class="o">.</span></code> prefix.</li>
  <li><strong>Versatility:</strong> The module can be used as a “Namespace” (direct call) or as a “Mixin” (included in classes).</li>
  <li><strong>Testability:</strong> It makes testing utility methods in isolation very easy since you can call them directly on the module.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[In Ruby, we often create modules to serve as libraries of utility functions. You’ve likely seen modules in various projects that use extend self right at the top. But what does it actually do, and why not just use def self.method?]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/understanding-extend-self.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/understanding-extend-self.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ActionView: relative_time_in_words</title><link href="https://www.mintbit.com/blog/actionview-relative-time-in-words/" rel="alternate" type="text/html" title="ActionView: relative_time_in_words" /><published>2026-01-07T12:37:00-05:00</published><updated>2026-01-07T12:37:00-05:00</updated><id>https://www.mintbit.com/blog/actionview-relative-time-in-words</id><content type="html" xml:base="https://www.mintbit.com/blog/actionview-relative-time-in-words/"><![CDATA[<p>Most Rails applications rely on the popular <a href="https://apidock.com/rails/v7.1.3.2/ActionView/Helpers/DateHelper/time_ago_in_words">time_ago_in_words</a> helper to show how much time has passed since an event. However, when dealing with future events or very recent dates, saying “in 20 hours” isn’t always the best way to communicate with a user.</p>

<p><a href="https://github.com/rails/rails/pull/55405">PR #55405</a> introduces the <code class="language-ruby highlighter-rouge"><span class="n">relative_time_in_words</span></code> helper, bringing much more natural language to our Views by handling dates as “today,” “yesterday,” or “tomorrow.”</p>

<h2 id="the-problem-lack-of-daily-context">The Problem: Lack of Daily Context</h2>

<p>The <code class="language-ruby highlighter-rouge"><span class="n">time_ago_in_words</span></code> helper focuses purely on time distance (minutes, hours, days). If a user has a trip scheduled for tomorrow, Rails might say “in about 20 hours.”</p>

<p>While technically correct, it’s not how humans speak. For a great user experience (UX), terms relative to the current day are much easier to process than performing mental calculations with hours.</p>

<h2 id="the-solution-native-natural-language">The Solution: Native Natural Language</h2>

<p><a href="https://apidock.com/rails/v8.1.1/ActionView/Helpers/DateHelper/relative_time_in_words">relative_time_in_words</a> examines the date and decides the best way to describe it in relation to “now,” prioritizing terms like yesterday, today, and tomorrow when appropriate.</p>

<h3 id="examples">Examples:</h3>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="rouge-code"><pre><span class="c1"># Assuming today is January 7, 2026</span>

<span class="n">relative_time_in_words</span><span class="p">(</span><span class="no">Date</span><span class="p">.</span><span class="nf">today</span><span class="p">)</span> 
<span class="c1"># =&gt; "today"</span>

<span class="n">relative_time_in_words</span><span class="p">(</span><span class="no">Date</span><span class="p">.</span><span class="nf">yesterday</span><span class="p">)</span> 
<span class="c1"># =&gt; "yesterday"</span>

<span class="n">relative_time_in_words</span><span class="p">(</span><span class="no">Date</span><span class="p">.</span><span class="nf">tomorrow</span><span class="p">)</span> 
<span class="c1"># =&gt; "tomorrow"</span>

<span class="n">relative_time_in_words</span><span class="p">(</span><span class="mi">2</span><span class="p">.</span><span class="nf">days</span><span class="p">.</span><span class="nf">ago</span><span class="p">)</span> 
<span class="c1"># =&gt; "2 days ago"</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-this-matters">Why This Matters</h2>

<ul>
  <li><strong>Better UX:</strong> Dates relative to the current day reduce the user’s cognitive load.</li>
  <li><strong>Consistency:</strong> Rails now standardizes how these terms are displayed across your entire application.</li>
  <li><strong>I18n Ready:</strong> As a native helper, it integrates seamlessly with the Rails internationalization system, making it easy to localize into any language.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[Most Rails applications rely on the popular time_ago_in_words helper to show how much time has passed since an event. However, when dealing with future events or very recent dates, saying “in 20 hours” isn’t always the best way to communicate with a user.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/actionview-relative-time-in-words.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/actionview-relative-time-in-words.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ActiveRecord: Understanding CurrentAttributes</title><link href="https://www.mintbit.com/blog/activerecord-understanding-currentattributes/" rel="alternate" type="text/html" title="ActiveRecord: Understanding CurrentAttributes" /><published>2026-01-05T05:56:00-05:00</published><updated>2026-01-05T05:56:00-05:00</updated><id>https://www.mintbit.com/blog/activerecord-understanding-currentattributes</id><content type="html" xml:base="https://www.mintbit.com/blog/activerecord-understanding-currentattributes/"><![CDATA[<p><code class="language-ruby highlighter-rouge"><span class="n">current_user</span></code> is a daily companion in Controllers and Views. However, the moment you need that information inside a Model to validate a business rule, things get tricky. Many developers end up passing the user as a parameter everywhere. But did you know Rails has a native solution for this?</p>

<p><a href="https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html">CurrentAttributes</a> is the official bridge to carry context from the Controller to the Model cleanly and safely.</p>

<h2 id="the-problem-argument-pollution">The Problem: Argument Pollution</h2>

<p>Imagine a method in your model that checks if a user can edit a record. Currently, it likely needs to receive the user as an argument:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">editable_by?</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">author</span> <span class="o">==</span> <span class="n">user</span> <span class="o">||</span> <span class="n">user</span><span class="p">.</span><span class="nf">admin?</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>While this works, if you have a call chain (Controller <code class="language-ruby highlighter-rouge"><span class="o">-&gt;</span></code> Service <code class="language-ruby highlighter-rouge"><span class="o">-&gt;</span></code> Model), you are forced to “carry” that user all the way down. This pollutes your method signatures and makes the code more rigid and harder to read.</p>

<h2 id="the-solution-centralizing-context">The Solution: Centralizing Context</h2>

<p><code class="language-ruby highlighter-rouge"><span class="no">CurrentAttributes</span></code> allows you to create a central repository for the request. Instead of passing the user manually, your Model simply asks Rails: “Who is the user right now?”.</p>

<h3 id="how-it-transforms-the-code">How it transforms the code:</h3>

<ol>
  <li><strong>Setup (app/models/current.rb):</strong>
    <div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Current</span> <span class="o">&lt;</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">CurrentAttributes</span>
  <span class="n">attribute</span> <span class="ss">:user</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div>    </div>
  </li>
  <li><strong>In the Controller:</strong> You set the value once at the beginning of the request.
    <div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="no">Current</span><span class="p">.</span><span class="nf">user</span> <span class="o">=</span> <span class="n">current_user</span> <span class="c1"># The user coming from your authentication system</span>
</pre></td></tr></tbody></table></code></pre></div>    </div>
  </li>
  <li><strong>In the Model:</strong> The method becomes much cleaner.
    <div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">can_be_edited?</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">author</span> <span class="o">==</span> <span class="no">Current</span><span class="p">.</span><span class="nf">user</span> <span class="o">||</span> <span class="no">Current</span><span class="p">.</span><span class="nf">user</span><span class="p">.</span><span class="nf">admin?</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div>    </div>
  </li>
</ol>

<h2 id="behind-the-scenes-thread-isolation">Behind the Scenes: Thread Isolation</h2>

<p>The most common concern is: “Is this safe? Won’t one user see another’s data?”. The answer is <strong>no</strong>.</p>

<p>Rails uses what is called Thread Isolation. Every request on your server runs in its own “bubble” (thread). Rails ensures that <code class="language-ruby highlighter-rouge"><span class="no">Current</span><span class="p">.</span><span class="nf">user</span></code> in Thread A never leaks into Thread B. Furthermore, at the end of every request, Rails automatically wipes (resets) this object, ensuring the next visitor starts with a clean slate.</p>

<h2 id="why-it-matters">Why It Matters</h2>

<ul>
  <li><strong>Declarative Code:</strong> Your methods focus on the “what” rather than “how” the data gets there.</li>
  <li><strong>Maintainability:</strong> Fewer arguments mean simpler refactoring and more focused tests.</li>
  <li><strong>Native Security:</strong> You leverage Rails’ own infrastructure to manage request state.</li>
</ul>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[current_user is a daily companion in Controllers and Views. However, the moment you need that information inside a Model to validate a business rule, things get tricky. Many developers end up passing the user as a parameter everywhere. But did you know Rails has a native solution for this?]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/activerecord-understanding-currentattributes.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/activerecord-understanding-currentattributes.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ActiveRecord: Consistent delete_all and update_all</title><link href="https://www.mintbit.com/blog/improving-consistency-between-delete-all-and-update-all/" rel="alternate" type="text/html" title="ActiveRecord: Consistent delete_all and update_all" /><published>2026-01-05T05:00:00-05:00</published><updated>2026-01-05T05:00:00-05:00</updated><id>https://www.mintbit.com/blog/improving-consistency-between-delete-all-and-update-all</id><content type="html" xml:base="https://www.mintbit.com/blog/improving-consistency-between-delete-all-and-update-all/"><![CDATA[<p>ActiveRecord excels at providing methods that balance convenience and performance. Among the most powerful are <a href="https://apidock.com/rails/v5.2.3/ActiveRecord/Relation/delete_all">delete_all</a> and <a href="https://apidock.com/rails/v5.2.3/ActiveRecord/Relation/update_all">update_all</a>, which execute bulk operations directly in SQL. However, until recently, they behaved inconsistently when used with certain query methods like <a href="https://apidock.com/rails/v5.2.3/ActiveRecord/QueryMethods/limit">limit</a> or <a href="https://apidock.com/rails/v5.2.3/ActiveRecord/QueryMethods/distinct">distinct</a>.</p>

<p>With the introduction of <a href="https://github.com/rails/rails/pull/54231">PR #54231</a>, Rails has unified how these methods validate queries before execution, putting an end to this long-standing inconsistency.</p>

<h2 id="the-problem-different-rules-for-similar-operations">The Problem: Different Rules for Similar Operations</h2>

<p>Even though both methods operate on Relations, Rails used to impose different restrictions on each.</p>

<p>For instance, if you tried to use <code class="language-ruby highlighter-rouge"><span class="n">update_all</span></code> on a query containing a <code class="language-ruby highlighter-rouge"><span class="n">limit</span></code> or an <code class="language-ruby highlighter-rouge"><span class="n">offset</span></code>, Rails would often throw an error (especially on databases that don’t natively support those clauses in an <code class="language-ruby highlighter-rouge"><span class="no">UPDATE</span></code> statement). Meanwhile, <code class="language-ruby highlighter-rouge"><span class="n">delete_all</span></code> might allow the operation or fail in a different way depending on how the query was constructed.</p>

<p>This discrepancy led to confusing bugs when refactoring logic—such as switching from a hard delete to a soft delete—expecting the API to handle the scopes identically.</p>

<h2 id="the-solution-standardized-validation">The Solution: Standardized Validation</h2>

<p>The new PR ensures that if an operation is invalid for the database (such as an <code class="language-ruby highlighter-rouge"><span class="no">UPDATE</span></code> or <code class="language-ruby highlighter-rouge"><span class="no">DELETE</span></code> combined with <code class="language-ruby highlighter-rouge"><span class="no">DISTINCT</span></code>, <code class="language-ruby highlighter-rouge"><span class="no">LIMIT</span></code>, or <code class="language-ruby highlighter-rouge"><span class="no">GROUP</span> <span class="no">BY</span></code> in certain contexts), both methods will now respond consistently.</p>

<h3 id="example">Example:</h3>

<p>Previously, you might encounter divergent behavior. Now, the validation is standardized:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="c1"># Attempting to update or delete within a complex scope</span>
<span class="n">scoped_query</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="ss">active: </span><span class="kp">true</span><span class="p">).</span><span class="nf">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

<span class="c1"># Both now follow the same validation rules</span>
<span class="n">scoped_query</span><span class="p">.</span><span class="nf">update_all</span><span class="p">(</span><span class="ss">status: </span><span class="s1">'archived'</span><span class="p">)</span> 
<span class="c1"># =&gt; Raises ActiveRecordError if the query is incompatible</span>

<span class="n">scoped_query</span><span class="p">.</span><span class="nf">delete_all</span> 
<span class="c1"># =&gt; Follows the exact same error logic as update_all</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-this-matters">Why This Matters</h2>

<p>This change is fundamental for three main reasons:</p>

<ul>
  <li><strong>Predictability:</strong> You no longer need to memorize which SQL clauses are permitted for <code class="language-ruby highlighter-rouge"><span class="n">delete_all</span></code> versus <code class="language-ruby highlighter-rouge"><span class="n">update_all</span></code>. If the Relation is invalid for bulk modification, it is invalid for both.</li>
  <li><strong>Data Safety:</strong> It prevents deletion operations from being executed partially or unexpectedly due to joins or limits being misinterpreted by the database.</li>
  <li><strong>Refactor-Friendly:</strong> It makes it much safer to toggle between “clearing data” and “flagging data as deleted” without worrying about breaking the underlying query logic.</li>
</ul>

<h2 id="the-rails-way">The Rails Way</h2>

<p>The focus here isn’t just about fixing a bug; it’s about reinforcing the ActiveRecord interface. By unifying these behaviors, the framework reduces the developer’s cognitive load, allowing you to trust that sibling methods will behave like siblings.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[ActiveRecord excels at providing methods that balance convenience and performance. Among the most powerful are delete_all and update_all, which execute bulk operations directly in SQL. However, until recently, they behaved inconsistently when used with certain query methods like limit or distinct.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/improving-consistency-between-delete-all-and-update-all.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/improving-consistency-between-delete-all-and-update-all.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Elegant Enum Sorting with in_order_of</title><link href="https://www.mintbit.com/blog/elegant-enum-sorting-with-in-order-of/" rel="alternate" type="text/html" title="Elegant Enum Sorting with in_order_of" /><published>2025-12-18T04:09:00-05:00</published><updated>2025-12-18T04:09:00-05:00</updated><id>https://www.mintbit.com/blog/elegant-enum-sorting-with-in-order-of</id><content type="html" xml:base="https://www.mintbit.com/blog/elegant-enum-sorting-with-in-order-of/"><![CDATA[<p><a href="https://api.rubyonrails.org/v5.1.7/classes/ActiveRecord/Enum.html">ActiveRecord enums</a> are a convenient way to map human-readable states to integers stored in the database. They make your models more expressive and your code easier to read. However, sorting records by enum values can be tricky when the default numeric order doesn’t match your business logic.</p>

<p>Starting with modern versions of Rails, <a href="https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-in_order_of">in_order_of</a> provides a clean and expressive way to define custom ordering directly in SQL—without resorting to database-specific functions or complex <code class="language-ruby highlighter-rouge"><span class="no">CASE</span></code> statements.</p>

<h2 id="what-are-activerecord-enums">What Are ActiveRecord Enums?</h2>

<p>Enums in ActiveRecord allow you to define a set of named values backed by integers in the database.</p>

<p>Example:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Order</span> <span class="o">&lt;</span> <span class="no">ApplicationRecord</span>
  <span class="n">enum</span> <span class="ss">status: </span><span class="p">{</span>
    <span class="ss">pending: </span><span class="mi">0</span><span class="p">,</span>
    <span class="ss">paid: </span><span class="mi">1</span><span class="p">,</span>
    <span class="ss">shipped: </span><span class="mi">2</span><span class="p">,</span>
    <span class="ss">cancelled: </span><span class="mi">3</span>
  <span class="p">}</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This gives you a friendly API while keeping storage efficient:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="no">Order</span><span class="p">.</span><span class="nf">paid</span>
<span class="n">order</span><span class="p">.</span><span class="nf">shipped?</span>
<span class="n">order</span><span class="p">.</span><span class="nf">status</span> <span class="c1"># =&gt; "pending"</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-default-ordering-isnt-always-enough">Why Default Ordering Isn’t Always Enough</h2>

<p>When you call:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="no">Order</span><span class="p">.</span><span class="nf">order</span><span class="p">(</span><span class="ss">:status</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Rails sorts by the underlying integer values. This works only if the enum definition matches the order you want to present in the UI or apply in business logic.</p>

<p>Often, that’s not the case. For example, you may want to prioritize shipped orders first, followed by paid ones, regardless of how the enum is defined.</p>

<h2 id="using-in_order_of-for-enum-sorting">Using in_order_of for Enum Sorting</h2>

<p>Rails provides <code class="language-ruby highlighter-rouge"><span class="n">in_order_of</span></code> as a high-level way to express custom ordering while keeping the sorting logic in SQL.</p>

<p>Example:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="no">Order</span><span class="p">.</span><span class="nf">in_order_of</span><span class="p">(</span>
  <span class="ss">:status</span><span class="p">,</span>
  <span class="p">[</span><span class="ss">:shipped</span><span class="p">,</span> <span class="ss">:paid</span><span class="p">,</span> <span class="ss">:pending</span><span class="p">,</span> <span class="ss">:cancelled</span><span class="p">]</span>
<span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This reads almost like plain English and clearly communicates intent.</p>

<p>Under the hood, Rails converts the enum values to their integer equivalents and generates the appropriate SQL to preserve the specified order.</p>

<h2 id="why-in_order_of-works-well-with-enums">Why in_order_of Works Well with Enums</h2>

<p><code class="language-ruby highlighter-rouge"><span class="n">in_order_of</span></code> integrates seamlessly with enums because:</p>

<ul>
  <li>You can use <strong>symbolic enum values</strong>, not integers</li>
  <li>The order is explicit and easy to change</li>
  <li>The query remains database-agnostic</li>
  <li>Sorting happens at the SQL level</li>
</ul>

<p>This makes it safer and more maintainable than hand-written SQL expressions.</p>

<h2 id="handling-partial-orders">Handling Partial Orders</h2>

<p>Sometimes, you only care about ordering a subset of enum values.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="no">Order</span><span class="p">.</span><span class="nf">in_order_of</span><span class="p">(</span><span class="ss">:status</span><span class="p">,</span> <span class="p">[</span><span class="ss">:shipped</span><span class="p">,</span> <span class="ss">:paid</span><span class="p">])</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Records with other statuses will still be returned, but they’ll appear after the specified values, following the database’s default order.</p>

<p>This is particularly useful when highlighting “important” states without excluding the rest.</p>

<h2 id="why-not-sort-in-ruby">Why Not Sort in Ruby?</h2>

<p>Sorting in Ruby using <code class="language-ruby highlighter-rouge"><span class="n">sort_by</span></code> or similar methods may seem convenient, but it has downsides:</p>

<ul>
  <li>All records must be loaded into memory</li>
  <li>Pagination breaks</li>
  <li>Performance degrades as data grows</li>
</ul>

<p><code class="language-ruby highlighter-rouge"><span class="n">in_order_of</span></code> avoids these problems by keeping ordering inside the database, where it belongs.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[ActiveRecord enums are a convenient way to map human-readable states to integers stored in the database. They make your models more expressive and your code easier to read. However, sorting records by enum values can be tricky when the default numeric order doesn’t match your business logic.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/elegant-enum-sorting-with-in-order-of.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/elegant-enum-sorting-with-in-order-of.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">The Difference Between resource and resources in Rails</title><link href="https://www.mintbit.com/blog/the-difference-between-resource-and-resources-in-rails/" rel="alternate" type="text/html" title="The Difference Between resource and resources in Rails" /><published>2025-12-17T08:11:00-05:00</published><updated>2025-12-17T08:11:00-05:00</updated><id>https://www.mintbit.com/blog/the-difference-between-resource-and-resources-in-rails</id><content type="html" xml:base="https://www.mintbit.com/blog/the-difference-between-resource-and-resources-in-rails/"><![CDATA[<p>Rails provides two closely related routing helpers — <code class="language-ruby highlighter-rouge"><span class="n">resource</span></code> and <code class="language-ruby highlighter-rouge"><span class="n">resources</span></code> — and although they look similar, they serve different purposes. Understanding the distinction between them is essential for designing clean, intention-revealing <a href="https://guides.rubyonrails.org/routing.html">routes in a Rails application</a>.</p>

<h2 id="what-are-resource-and-resources">What Are resource and resources?</h2>

<p>Both helpers are used in <code class="language-ruby highlighter-rouge"><span class="n">config</span><span class="o">/</span><span class="n">routes</span><span class="p">.</span><span class="nf">rb</span></code> to generate RESTful routes. The key difference lies in <strong>whether the resource is singular or plural</strong>, and that directly affects the URLs and controller actions Rails creates for you.</p>

<h2 id="resources-plural-resources">resources: Plural Resources</h2>

<p><a href="https://guides.rubyonrails.org/routing.html#resources-on-the-web">resources</a> is the most commonly used helper. It represents a <strong>collection of objects</strong>, where each item has its own identifier (usually an <code class="language-ruby highlighter-rouge"><span class="nb">id</span></code>).</p>

<p>Example:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">resources</span> <span class="ss">:articles</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This generates the full set of RESTful routes:</p>

<table>
  <thead>
    <tr>
      <th>HTTP Verb</th>
      <th>Path</th>
      <th>Controller#Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>GET</td>
      <td>/articles</td>
      <td>articles#index</td>
    </tr>
    <tr>
      <td>GET</td>
      <td>/articles/new</td>
      <td>articles#new</td>
    </tr>
    <tr>
      <td>POST</td>
      <td>/articles</td>
      <td>articles#create</td>
    </tr>
    <tr>
      <td>GET</td>
      <td>/articles/:id</td>
      <td>articles#show</td>
    </tr>
    <tr>
      <td>GET</td>
      <td>/articles/:id/edit</td>
      <td>articles#edit</td>
    </tr>
    <tr>
      <td>PATCH</td>
      <td>/articles/:id</td>
      <td>articles#update</td>
    </tr>
    <tr>
      <td>DELETE</td>
      <td>/articles/:id</td>
      <td>articles#destroy</td>
    </tr>
  </tbody>
</table>

<p>You use <code class="language-ruby highlighter-rouge"><span class="n">resources</span></code> when:</p>

<ul>
  <li>There are <strong>many records</strong></li>
  <li>Each record is accessed by an <strong>ID</strong></li>
  <li>You need collection-level actions like <code class="language-ruby highlighter-rouge"><span class="n">index</span></code></li>
</ul>

<p>Typical examples include posts, comments, products, and users.</p>

<h2 id="resource-singular-resources">resource: Singular Resources</h2>

<p><a href="https://guides.rubyonrails.org/routing.html#singular-resources">resource</a> (singular) represents <strong>a single object that does not need an ID</strong> in the URL. Rails assumes there is only one instance of that resource per context (often per user).</p>

<p>Example:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">resource</span> <span class="ss">:profile</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This generates a smaller set of routes:</p>

<table>
  <thead>
    <tr>
      <th>HTTP Verb</th>
      <th>Path</th>
      <th>Controller#Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>GET</td>
      <td>/profile/new</td>
      <td>profiles#new</td>
    </tr>
    <tr>
      <td>POST</td>
      <td>/profile</td>
      <td>profiles#create</td>
    </tr>
    <tr>
      <td>GET</td>
      <td>/profile</td>
      <td>profiles#show</td>
    </tr>
    <tr>
      <td>GET</td>
      <td>/profile/edit</td>
      <td>profiles#edit</td>
    </tr>
    <tr>
      <td>PATCH</td>
      <td>/profile</td>
      <td>profiles#update</td>
    </tr>
    <tr>
      <td>DELETE</td>
      <td>/profile</td>
      <td>profiles#destroy</td>
    </tr>
  </tbody>
</table>

<p>Notice what’s missing:</p>

<ul>
  <li>No <code class="language-ruby highlighter-rouge"><span class="n">index</span></code> route</li>
  <li>No <code class="language-ruby highlighter-rouge"><span class="ss">:id</span></code> segment in the URL</li>
</ul>

<p>You use <code class="language-ruby highlighter-rouge"><span class="n">resource</span></code> when:</p>

<ul>
  <li>There is <strong>only one instance</strong></li>
  <li>The resource is <strong>implicitly identified</strong></li>
  <li>An <code class="language-ruby highlighter-rouge"><span class="nb">id</span></code> would be redundant</li>
</ul>

<p>Common examples include user profiles, dashboards, shopping carts, or account settings.</p>

<h2 id="controller-naming-still-uses-plural">Controller Naming Still Uses Plural</h2>

<p>Even when using <code class="language-ruby highlighter-rouge"><span class="n">resource</span></code>, Rails still expects a <strong>plural controller name</strong>:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">resource</span> <span class="ss">:profile</span>
<span class="c1"># maps to ProfilesController</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This keeps controller naming consistent across the framework.</p>

<h2 id="choosing-the-right-one">Choosing the Right One</h2>

<p>A good rule of thumb:</p>

<ul>
  <li>Use <strong><code class="language-ruby highlighter-rouge"><span class="n">resources</span></code></strong> when you can say:
<em>“There are many of these.”</em></li>
  <li>Use <strong><code class="language-ruby highlighter-rouge"><span class="n">resource</span></code></strong> when you can say:
<em>“There is only one of these per scope.”</em></li>
</ul>

<p>For example:</p>

<ul>
  <li>A user <strong>has many</strong> posts → <code class="language-ruby highlighter-rouge"><span class="n">resources</span> <span class="ss">:posts</span></code></li>
  <li>A user <strong>has one</strong> profile → <code class="language-ruby highlighter-rouge"><span class="n">resource</span> <span class="ss">:profile</span></code></li>
</ul>

<h2 id="why-this-matters">Why This Matters</h2>

<p>Choosing between <code class="language-ruby highlighter-rouge"><span class="n">resource</span></code> and <code class="language-ruby highlighter-rouge"><span class="n">resources</span></code> is more than syntax — it communicates intent. Singular routes make your URLs cleaner, your controllers simpler, and your application easier to reason about.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[Rails provides two closely related routing helpers — resource and resources — and although they look similar, they serve different purposes. Understanding the distinction between them is essential for designing clean, intention-revealing routes in a Rails application.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/the-difference-between-resource-and-resources-in-rails.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/the-difference-between-resource-and-resources-in-rails.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Send Emails in Bulk with deliver_all_later</title><link href="https://www.mintbit.com/blog/send-emails-in-bulk-with-deliver-all-later/" rel="alternate" type="text/html" title="Send Emails in Bulk with deliver_all_later" /><published>2025-12-05T09:17:00-05:00</published><updated>2025-12-05T09:17:00-05:00</updated><id>https://www.mintbit.com/blog/send-emails-in-bulk-with-deliver-all-later</id><content type="html" xml:base="https://www.mintbit.com/blog/send-emails-in-bulk-with-deliver-all-later/"><![CDATA[<p>This year, Rails introduced a new method for <a href="https://edgeapi.rubyonrails.org/classes/ActionMailer.html">ActionMailer</a> called <a href="https://edgeapi.rubyonrails.org/classes/ActionMailer.html#method-c-deliver_all_later">deliver_all_later</a>. This feature simplifies enqueuing multiple emails for delivery through Active Job. Instead of sending each email individually, you can now enqueue a batch of emails, allowing them to be sent asynchronously when their respective jobs run.</p>

<h2 id="what-is-deliver_all_later">What is deliver_all_later?</h2>

<p><code class="language-ruby highlighter-rouge"><span class="n">deliver_all_later</span></code> enqueues many emails at once. When each job is executed, it sends the email using <code class="language-ruby highlighter-rouge"><span class="n">deliver_now</span></code>. This improves performance by reducing the number of round-trips to the queue datastore, especially when dealing with a large number of emails.</p>

<h2 id="how-does-it-work">How Does It Work?</h2>

<p>To use <code class="language-ruby highlighter-rouge"><span class="n">deliver_all_later</span></code>, you can pass an array of email deliveries:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">user_emails</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">all</span><span class="p">.</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">user</span><span class="o">|</span> <span class="no">Notifier</span><span class="p">.</span><span class="nf">welcome</span><span class="p">(</span><span class="n">user</span><span class="p">)</span> <span class="p">}</span>
<span class="no">ActionMailer</span><span class="p">.</span><span class="nf">deliver_all_later</span><span class="p">(</span><span class="n">user_emails</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>You can even specify a custom queue if needed:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="no">ActionMailer</span><span class="p">.</span><span class="nf">deliver_all_later</span><span class="p">(</span><span class="n">user_emails</span><span class="p">,</span> <span class="ss">queue: :my_queue</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This new feature is particularly useful for large-scale email delivery, such as when you’re sending out notifications to thousands of users. By batching email deliveries, you reduce the load on your application and improve overall efficiency.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[This year, Rails introduced a new method for ActionMailer called deliver_all_later. This feature simplifies enqueuing multiple emails for delivery through Active Job. Instead of sending each email individually, you can now enqueue a batch of emails, allowing them to be sent asynchronously when their respective jobs run.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/send-emails-in-bulk-with-deliver-all-later.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/send-emails-in-bulk-with-deliver-all-later.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Match HTTP Methods with current_page?</title><link href="https://www.mintbit.com/blog/match-http-methods-with-current-page/" rel="alternate" type="text/html" title="Match HTTP Methods with current_page?" /><published>2025-12-05T07:16:00-05:00</published><updated>2025-12-05T07:16:00-05:00</updated><id>https://www.mintbit.com/blog/match-http-methods-with-current-page</id><content type="html" xml:base="https://www.mintbit.com/blog/match-http-methods-with-current-page/"><![CDATA[<p>When building a Rails app, you often need to create navigation links that highlight the current page. However, Rails’ default <code class="language-ruby highlighter-rouge"><span class="n">current_page?</span></code> helper only works with <code class="language-ruby highlighter-rouge"><span class="no">GET</span></code> and <code class="language-ruby highlighter-rouge"><span class="no">HEAD</span></code> requests. This can be problematic when dealing with forms or actions that use other HTTP methods like <code class="language-ruby highlighter-rouge"><span class="no">POST</span></code>, <code class="language-ruby highlighter-rouge"><span class="no">PUT</span></code>, or <code class="language-ruby highlighter-rouge"><span class="no">PATCH</span></code>. This feature was introduced in <a href="https://github.com/rails/rails/pull/55286">this PR</a></p>

<h2 id="the-problem">The Problem</h2>

<p>For example, if you have a page for viewing posts and another for creating posts, both share the same URL (<code class="language-ruby highlighter-rouge"><span class="sr">/posts</span></code>). The only difference is the HTTP method used (<code class="language-ruby highlighter-rouge"><span class="no">GET</span></code> for viewing and <code class="language-ruby highlighter-rouge"><span class="no">POST</span></code> for creating). With the default <code class="language-ruby highlighter-rouge"><span class="n">current_page?</span></code> helper, it’s difficult to highlight the correct navigation link when a user submits a form to create a post.</p>

<h2 id="the-solution">The Solution</h2>

<p>Rails now allows you to pass a <code class="language-ruby highlighter-rouge"><span class="nb">method</span></code> argument to <code class="language-ruby highlighter-rouge"><span class="n">current_page?</span></code>, enabling you to match specific HTTP methods when checking the current page. This means you can distinguish between actions that share the same URL but differ by HTTP method.</p>

<p>Here’s how to use it:</p>

<div class="language-erb highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="nt">&lt;ul</span> <span class="na">class=</span><span class="s">"nav nav-tabs"</span><span class="nt">&gt;</span>
  <span class="nt">&lt;li</span> <span class="na">class=</span><span class="s">"nav-item"</span><span class="nt">&gt;</span>
    <span class="cp">&lt;%=</span> <span class="n">link_to</span> <span class="s2">"All posts"</span><span class="p">,</span> <span class="n">posts_path</span><span class="p">,</span> <span class="ss">class: </span><span class="p">[</span><span class="s2">"nav-link"</span><span class="p">,</span> <span class="p">{</span> <span class="ss">active: </span><span class="n">current_page?</span><span class="p">(</span><span class="n">posts_path</span><span class="p">)</span> <span class="p">}]</span> <span class="cp">%&gt;</span>
  <span class="nt">&lt;/li&gt;</span>
  <span class="nt">&lt;li</span> <span class="na">class=</span><span class="s">"nav-item"</span><span class="nt">&gt;</span>
    <span class="cp">&lt;%=</span> <span class="n">link_to</span> <span class="s2">"Create post"</span><span class="p">,</span> <span class="n">new_post_path</span><span class="p">,</span> <span class="ss">class: </span><span class="p">[</span><span class="s2">"nav-link"</span><span class="p">,</span> <span class="p">{</span> <span class="ss">active: </span><span class="n">current_page?</span><span class="p">(</span><span class="n">new_post_path</span><span class="p">)</span> <span class="o">||</span> <span class="n">current_page?</span><span class="p">(</span><span class="n">posts_path</span><span class="p">,</span> <span class="ss">method: :post</span><span class="p">)</span> <span class="p">}]</span> <span class="cp">%&gt;</span>
  <span class="nt">&lt;/li&gt;</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="why-it-matters">Why It Matters</h2>

<p>This simple addition makes it easier to highlight the right link, whether you’re viewing posts (<code class="language-ruby highlighter-rouge"><span class="no">GET</span> <span class="sr">/posts</span></code>) or creating a new post (<code class="language-ruby highlighter-rouge"><span class="no">POST</span> <span class="sr">/posts</span></code>). It improves your app’s navigation and helps you avoid unnecessary workarounds.</p>

<p>By adding support for HTTP methods in <code class="language-ruby highlighter-rouge"><span class="n">current_page?</span></code>, Rails makes it simpler to handle navigation for actions that share the same URL. If you’re working with forms or actions that use multiple HTTP methods, this feature will save you time and help create cleaner, more intuitive navigation.</p>]]></content><author><name>Morgana Borges</name></author><category term="Ruby on Rails" /><summary type="html"><![CDATA[When building a Rails app, you often need to create navigation links that highlight the current page. However, Rails’ default current_page? helper only works with GET and HEAD requests. This can be problematic when dealing with forms or actions that use other HTTP methods like POST, PUT, or PATCH. This feature was introduced in this PR]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.mintbit.com/assets/posts/match-http-methods-with-current-page.png" /><media:content medium="image" url="https://www.mintbit.com/assets/posts/match-http-methods-with-current-page.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>