<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michel Sánchez Montells</title>
    <description>The latest articles on DEV Community by Michel Sánchez Montells (@montells).</description>
    <link>https://dev.to/montells</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F413459%2Fd7b3a7ed-daca-45ce-8482-b519d9fe5a35.jpeg</url>
      <title>DEV Community: Michel Sánchez Montells</title>
      <link>https://dev.to/montells</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/montells"/>
    <language>en</language>
    <item>
      <title>Copy &amp; Paste PostgreSQL Query Results into Google Sheets (No CSV Needed)</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Fri, 15 Aug 2025 10:48:14 +0000</pubDate>
      <link>https://dev.to/montells/copy-paste-postgresql-query-results-into-google-sheets-no-csv-needed-3omb</link>
      <guid>https://dev.to/montells/copy-paste-postgresql-query-results-into-google-sheets-no-csv-needed-3omb</guid>
      <description>&lt;p&gt;Sometimes, as developers, we need to share query results with teammates maybe to explain a bug, show app behavior, or provide visibility into some data. Often, this means copying directly from the terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  But here’s the catch: 😕
&lt;/h2&gt;

&lt;p&gt;Pasting raw SQL output into a text editor or a spreadsheet usually ends in a chaos  😫😤😞. Everything dumped into a single cell, broken formatting, unreadable messes 🥴.&lt;/p&gt;

&lt;p&gt;And yet, putting this data into Google Sheets is super helpful: it’s clear, searchable, and collaborative. But exporting to CSV just for that? Too much friction.&lt;/p&gt;

&lt;p&gt;Here’s a dead-simple way to copy query results from psql and paste them &lt;strong&gt;cleanly&lt;/strong&gt; into Google Sheets—&lt;strong&gt;no CSVs, no cleanup, no formatting pain.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔧 How to do it (in seconds):
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Open your terminal and start &lt;code&gt;psql&lt;/code&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql -U your_user -d your_database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Set output to tab-separated format (what Google Sheets understands natively):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\x off
\pset format unaligned
\pset fieldsep '\t'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Run your query:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, name, email FROM users LIMIT 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Copy the result from your terminal and paste it directly into Google Sheets.
&lt;/h3&gt;

&lt;p&gt;😲 Each &lt;strong&gt;column&lt;/strong&gt; goes perfectly into its &lt;strong&gt;own cell&lt;/strong&gt;&lt;br&gt;
😄 Each &lt;strong&gt;row&lt;/strong&gt; drops nicely into a &lt;strong&gt;separate row&lt;/strong&gt;&lt;br&gt;
😌 No cleaning, no reformatting, just pure copy/paste joy&lt;/p&gt;

&lt;p&gt;No CSV. No cleaning. No pain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hope this saves you time!&lt;/strong&gt;&lt;br&gt;
If it did, give it a 💚 or share it with your team.&lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
    <item>
      <title>How to Upgrade PostgreSQL with Docker Compose and Bitbucket Pipelines (Step-by-Step Guide)</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Wed, 30 Jul 2025 13:44:52 +0000</pubDate>
      <link>https://dev.to/montells/upgrade-postgres-when-using-docker-compose-4di4</link>
      <guid>https://dev.to/montells/upgrade-postgres-when-using-docker-compose-4di4</guid>
      <description>&lt;p&gt;Working in a old &lt;a href="https://rubyonrails.org/" rel="noopener noreferrer"&gt;RoR&lt;/a&gt; application I was asked to updgrade &lt;a href="https://www.postgresql.org/docs/9.5/index.html" rel="noopener noreferrer"&gt;Postgresql&lt;/a&gt; from 9.5 up to the latest possible. The RoR application is deployed on premises via &lt;a href="https://docs.docker.com/compose/compose-file/compose-file-v2/" rel="noopener noreferrer"&gt;Docker Compose&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The postgres documentation explains and recomends:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;We always recommend that all users run the latest available minor release for whatever major version is in use.&lt;/strong&gt; &lt;a href="https://www.postgresql.org/support/versioning/" rel="noopener noreferrer"&gt;https://www.postgresql.org/support/versioning/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fortunately my current version is the last minor version for 9.5 -&amp;gt; 9.5.25. Then I will upgrade from 9.5 to 9.6. &lt;/p&gt;

&lt;p&gt;Some reasonable steps for migrating in this conditions are described in: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://christierney.com/2022/03/09/upgrading-postgresql-on-docker-compose/" rel="noopener noreferrer"&gt;https://christierney.com/2022/03/09/upgrading-postgresql-on-docker-compose/&lt;/a&gt; By &lt;a href="https://christierney.com/" rel="noopener noreferrer"&gt;Chris Tierney&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="https://betterprogramming.pub/how-to-upgrade-your-postgresql-version-using-docker-d1e81dbbbdf9" rel="noopener noreferrer"&gt;https://betterprogramming.pub/how-to-upgrade-your-postgresql-version-using-docker-d1e81dbbbdf9&lt;/a&gt; By &lt;a href="https://medium.com/@josepostiga?source=post_page-----d1e81dbbbdf9--------------------------------" rel="noopener noreferrer"&gt;José Postiga&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, I want to achieve my goal without ssh into the server and manual steps. I would like to achieve this setting up a manual CI pipeline|job and pushing some simple changes like renaming docker-compose volumes as the unique human intervention making this process as repeatable and as less error prompt as possible 🙏.&lt;/p&gt;

&lt;p&gt;In this case &lt;a href="https://bitbucket.org/product/features/pipelines" rel="noopener noreferrer"&gt;Bitbucket-Pipeline&lt;/a&gt;. Any way this approach should work with some adapts on whatever CI service allows to execution of custom bash commands. (Jenkins, GitlabCi, among others).&lt;/p&gt;

&lt;p&gt;Hopefully, after the first round of this upgrading cycle I will be able to perform the rest of the upgrades just changing numbers 🙏.&lt;/p&gt;

&lt;p&gt;After said all my preconditions, requirements, and ambitious. Let's put the hands on the keyboard.&lt;/p&gt;

&lt;p&gt;This is my docker-compose.yml simplified for the sake of simplicity. Only services and needed details for my purpose are shown.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '2.1'  
services:  
  web:  
    image: my_ror_image
    depends_on:  
      - pg  
  pg:  
    image: postgres:9.5-alpine  
    volumes:  
      - ./tmp/db:/var/lib/postgresql/data  
    environment:  
      - POSTGRES_PASSWORD=pass  
      - POSTGRES_USER=user  
      - POSTGRES_DB=db_prod_pass  
    hostname: pg  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 1: Adapt my docker-compose in the way that the specs involve the version.
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '2.1'  
services:  
  web:  
    image: my_ror_image
    depends_on:  
      - pg  
  pg:  
    image: postgres:9.5-alpine  
    volumes:  
      - ./tmp/db_9.5:/var/lib/postgresql/data  
    environment:  
      - POSTGRES_PASSWORD=pass  
      - POSTGRES_USER=user  
      - POSTGRES_DB=db_prod_pass  
    hostname: pg  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, the name of host directory mapped to the container directory is using the &lt;strong&gt;version&lt;/strong&gt;. It will allow me to conserve both versions in the automatic upgrade process in order to being able to rollback in case of failure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Deploy the app with the current approach
&lt;/h3&gt;

&lt;p&gt;Here before doing any upgrade we will re-deploy our app using different folder for storing/sharing data with/between our host and the pg container.&lt;/p&gt;

&lt;p&gt;ssh to your server, create the directory with the name including the version, and copy all content of the previous pg-data directory.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;mkdir &amp;lt;app-dir&amp;gt;/tmp/db_9.5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cp &amp;lt;app-dir&amp;gt;/tmp/db &amp;lt;app-dir&amp;gt;/tmp/db_9.5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;redeploy the last version including addapted docker-compose&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The new deployed app will take data from the directory that includes the version on the name.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is step is manual but will never be repated, it is part of the&lt;br&gt;
preparation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 3: Create a docker-compose file for the upgrade process
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '2.1'
services:
  new_pg:
    image: postgres:${VERSION_TO}-alpine
    volumes:
      - ./tmp/db_${VERSION_TO}:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=user
      - POSTGRES_DB=db_labs

  old_pg:
    image: postgres:${VERSION_FROM}-alpine
    volumes:
      - ./tmp/db_${VERSION_FROM}:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=user
      - POSTGRES_DB=db_labs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we are for convinient using environment variables for set the involved versions. This docker compose will start two containers with the both FROM and TO versions of postgres. We will use them for dumping loading 😉&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: The Bitbucket Pipeline
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;upgrade_postgres:
  - variables:
      - name: DeployCode
        default: staging
        allowed-values:
          - production
          - staging
          - labs
      - name: UP_PG_FROM
        default: '9.6'
        allowed-values:
          - '9.6'
          - '10'
          - '11'
          - '12'
          - '13'
          - '14'
      - name: UP_PG_TO
        default: '10'
        allowed-values:
          - '10'
          - '11'
          - '12'
          - '13'
          - '14'
          - '15'
  - step:
      name: 'upgrade-postgres'
      script:
        - *prepare_ssh
        - scp -o StrictHostKeyChecking=no ./docker-compose-${DeployCode}.yml ${SSH_SERVER_ADDRESS}:/web-app/
        - scp -o StrictHostKeyChecking=no ./docker-compose-pg-upgrade.yml ${SSH_SERVER_ADDRESS}:/web-app/
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; docker-compose -f docker-compose-${DeployCode}.yml down --remove-orphans"
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; VERSION_FROM=$UP_PG_FROM VERSION_TO=$UP_PG_TO docker-compose -f docker-compose-pg-upgrade.yml up -d"
        - ssh_connect_to_deployment_server "mkdir -p /web-app/tmp/db_load_$UP_PG_TO"
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; docker-compose -f docker-compose-pg-upgrade.yml exec -T old_pg mkdir db_dump_$UP_PG_FROM"
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; docker-compose -f docker-compose-pg-upgrade.yml exec -T new_pg mkdir /db_load_$UP_PG_TO"
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; docker-compose -f docker-compose-pg-upgrade.yml exec -T old_pg pg_dumpall -U user -f /db_dump_$UP_PG_FROM/dump.sql"
        - ssh_connect_to_deployment_server "docker cp wm-web-app-${DeployCode}_old_pg_1:/db_dump_$UP_PG_FROM/dump.sql /web-app/tmp/db_load_$UP_PG_TO/dump.sql"
        - ssh_connect_to_deployment_server "docker cp /web-app/tmp/db_load_$UP_PG_TO/dump.sql wm-web-app-${DeployCode}_new_pg_1:/db_load_$UP_PG_TO/dump.sql"
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; docker-compose -f docker-compose-pg-upgrade.yml exec -T new_pg psql -U user -d postgres -f /db_load_$UP_PG_TO/dump.sql"
        - ssh_connect_to_deployment_server "cd /web-app &amp;amp;&amp;amp; docker-compose -f docker-compose-pg-upgrade.yml down --remove-orphans"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This bitbucket pipeline stop the our online application. Start two postgres container with both versions. Dump the FROM version and load it in the TO version, helping with &lt;code&gt;docker cp&lt;/code&gt; feature and &lt;a href="https://dev.to/montells/parametrize-your-custom-bitbucket-pipeline-with-variables-hdp"&gt;bitbucket pipeline variables&lt;/a&gt; feature.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ℹ️ Just make sure the &lt;code&gt;VERSION_FROM&lt;/code&gt; and &lt;code&gt;VERSION_TO&lt;/code&gt; values match the&lt;br&gt;
subfolder names in your &lt;code&gt;tmp/&lt;/code&gt; volume folder. That’s what keeps&lt;br&gt;
everything clean and rollback-ready.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 5: Run this pipeline manually on bitbucket.
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  Step 6: Update your production docker compose and redeploy.
&lt;/h3&gt;

&lt;p&gt;After a successful upgrade:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Update your production &lt;code&gt;docker-compose&lt;/code&gt; to use the new version and its corresponding volume:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; volumes:
      - ./tmp/db_10:/var/lib/postgresql/data
 image: postgres:10-alpine
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commit this change and redeploy.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;What started as a scary task of upgrading an old Postgres DB turned out to be a repeatable and safe process. With just a few abstractions (a second Docker Compose file and a CI job), I turned it into a hands-off operation. Next time I want to upgrade, all I’ll do is change the &lt;code&gt;FROM&lt;/code&gt; and &lt;code&gt;TO&lt;/code&gt; versions and hit “Run pipeline”.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Automation is not just about speed — it’s about &lt;strong&gt;reliability&lt;/strong&gt; and &lt;strong&gt;peace of mind&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hope this helps you on your own upgrade journey! 🙌  &lt;/p&gt;

</description>
      <category>postgres</category>
      <category>docker</category>
      <category>bitbucket</category>
      <category>cicd</category>
    </item>
    <item>
      <title>Writing Reusable and Performant Scopes in Rails with .merge</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Sat, 19 Jul 2025 10:15:07 +0000</pubDate>
      <link>https://dev.to/montells/writing-reusable-and-performant-scopes-in-rails-with-merge-54ka</link>
      <guid>https://dev.to/montells/writing-reusable-and-performant-scopes-in-rails-with-merge-54ka</guid>
      <description>&lt;h2&gt;
  
  
  How to combine scopes in Rails in a DRY and efficient way?
&lt;/h2&gt;

&lt;p&gt;In my day-to-day work with Rails, I use scopes in almost every model.  They’re the perfect tool to encapsulate filtering logic, keep code clean, and build complex queries in a readable way.&lt;/p&gt;

&lt;p&gt;One of the most common cases:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I want the published posts from &lt;strong&gt;active&lt;/strong&gt; authors &lt;strong&gt;living in America&lt;/strong&gt;, &lt;strong&gt;except&lt;/strong&gt; if they write in &lt;strong&gt;Portuguese or English&lt;/strong&gt;."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nothing fancy, right?&lt;br&gt;
But here’s where many of us (myself included) hit a common question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Should I chain scopes directly? Or use &lt;code&gt;.merge&lt;/code&gt;? Does it matter?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Spoiler: &lt;strong&gt;It does matter&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  The setup: authors and posts
&lt;/h2&gt;

&lt;p&gt;In this example, we have two classic models: &lt;code&gt;Author&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/models/author.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;

  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:from_america&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;continent: &lt;/span&gt;&lt;span class="s1"&gt;'America'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:excluding_languages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;langs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;language: &lt;/span&gt;&lt;span class="n"&gt;langs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;active: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;# Chained and reusable scope&lt;/span&gt;
  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:american_active_no_pt_en&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;from_america&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;excluding_languages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sx"&gt;%w[pt en]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# app/models/post.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:author&lt;/span&gt;
  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:published&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;published: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The requirement
&lt;/h2&gt;

&lt;p&gt;Our client (or product team, or pragmatic mind) wants this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Give me all published posts by active authors who live in America and do not write in Portuguese or English."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And as good developers, we want this to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  DRY&lt;/li&gt;
&lt;li&gt;  Performant&lt;/li&gt;
&lt;li&gt;  Reusable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, we try different approaches.&lt;/p&gt;




&lt;h2&gt;
  
  
  First solution: DRY, but with a subquery (less performance)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:by_active_american_authors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;author_id:  &lt;/span&gt;&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;american_active_no_pt_en&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Typical usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="sb"&gt;`# app/models/post.rb  
Post.published.by_active_american_authors`&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Generated query:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;  
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"published"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;TRUE&lt;/span&gt;  
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"author_id"&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt; 
                             &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt; 
                             &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"continent"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s1"&gt;'America'&lt;/span&gt;  
                             &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"active"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;TRUE&lt;/span&gt;  
                             &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"language"&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt;  &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="nv"&gt;` 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is super DRY.&lt;br&gt;&lt;br&gt;
We reuse &lt;code&gt;Author.american_active_no_pt_en&lt;/code&gt; without duplicating logic.&lt;br&gt;
&lt;strong&gt;But:&lt;/strong&gt; it uses a subquery in the &lt;code&gt;WHERE IN&lt;/code&gt;, which can become expensive if there are many authors or if that scope includes additional joins.&lt;/p&gt;


&lt;h2&gt;
  
  
  Second solution: efficient, but repetitive
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:by_active_american_authors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="ss"&gt;authors: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;continent:  &lt;/span&gt;&lt;span class="s1"&gt;'America'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;active:  &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="ss"&gt;authors: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;language:  &lt;/span&gt;&lt;span class="sx"&gt;%w[pt en]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Typical usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;published&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;by_active_american_authors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Generated query:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;  
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt; 
&lt;span class="k"&gt;INNER&lt;/span&gt;  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"author_id"&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"continent"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s1"&gt;'America'&lt;/span&gt;  
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"active"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;TRUE&lt;/span&gt;  
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"language"&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt;  &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"published"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;TRUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;` 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the query is very efficient: the &lt;code&gt;JOIN&lt;/code&gt; lets the DB use indexes.&lt;br&gt;
But we’re duplicating the logic already defined in &lt;code&gt;Author.american_active_no_pt_en&lt;/code&gt;.&lt;br&gt;
Every time that logic changes, we have to remember to update it in multiple places.&lt;br&gt;&lt;br&gt;
And we all know how those stories end.&lt;/p&gt;


&lt;h2&gt;
  
  
  Third solution: DRY and efficient with &lt;code&gt;.merge&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:by_active_american_authors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;american_active_no_pt_en&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;` 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Typical usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;published&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;by_active_american_authors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Generated query (same as above):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;  
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt; 
&lt;span class="k"&gt;INNER&lt;/span&gt;  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"author_id"&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"continent"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s1"&gt;'America'&lt;/span&gt;  
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"active"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;TRUE&lt;/span&gt;  
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"authors"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"language"&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt;  &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"published"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;TRUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;.merge&lt;/code&gt;, we get the best of both worlds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  ✅ We reuse the scope from the &lt;code&gt;Author&lt;/code&gt; model, keeping the code clean.&lt;/li&gt;
&lt;li&gt;  ✅ We generate an optimal query, with no subqueries.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  So… what does &lt;code&gt;.merge&lt;/code&gt; actually do?
&lt;/h2&gt;

&lt;p&gt;We’ve seen &lt;code&gt;.merge&lt;/code&gt; gives us both efficiency and reuse.&lt;br&gt;&lt;br&gt;
But if you’re like me, you want to know &lt;strong&gt;how&lt;/strong&gt; it works and when it can fail.&lt;/p&gt;
&lt;h3&gt;
  
  
  Does Rails run the scope first and then mix the results?
&lt;/h3&gt;

&lt;p&gt;No. &lt;code&gt;.merge&lt;/code&gt; doesn’t run anything separately.&lt;/p&gt;

&lt;p&gt;Rails &lt;strong&gt;fuses&lt;/strong&gt;(merge) the SQL conditions of the second scope into the first one.&lt;br&gt;&lt;br&gt;
It’s not that &lt;code&gt;Author.american_active_no_pt_en&lt;/code&gt; gets executed and its results passed over.&lt;/p&gt;

&lt;p&gt;Here’s what’s really happening:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="sb"&gt;`Post.joins(:author).merge(Author.active)`&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rails interprets this as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Okay, you're doing a &lt;code&gt;join&lt;/code&gt; with &lt;code&gt;authors&lt;/code&gt;,  and you want to apply conditions from this other scope in &lt;code&gt;Author&lt;/code&gt;.  Let me mix that into one single &lt;code&gt;ActiveRecord::Relation&lt;/code&gt;.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The result is a single SQL query.&lt;/p&gt;




&lt;h3&gt;
  
  
  What exactly gets copied?
&lt;/h3&gt;

&lt;p&gt;Rails takes from the second scope things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;WHERE&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;JOIN&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;ORDER&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;LIMIT&lt;/code&gt;, &lt;code&gt;OFFSET&lt;/code&gt;, &lt;code&gt;GROUP BY&lt;/code&gt; (with some caveats)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And it adds them into the &lt;code&gt;ActiveRecord::Relation&lt;/code&gt; you’re building with &lt;code&gt;Post&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if I try &lt;code&gt;.merge&lt;/code&gt; without a &lt;code&gt;join&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# ❌` &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will raise an error.&lt;br&gt;&lt;br&gt;
Rails doesn’t know how to apply &lt;code&gt;authors&lt;/code&gt; conditions in a query that only involves &lt;code&gt;posts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The right way is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="sb"&gt;` 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Can &lt;code&gt;.merge&lt;/code&gt; break things?
&lt;/h2&gt;

&lt;p&gt;Yes. Here are real situations where &lt;code&gt;.merge&lt;/code&gt; can cause trouble:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. When the merged scope uses &lt;code&gt;select&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This overrides the base model's select  &lt;/span&gt;
&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="sb"&gt;` 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. When using aliases or multiple joins&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:editor&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# ⚠️ Rails may get confused` &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. When the scope uses &lt;code&gt;group&lt;/code&gt;, &lt;code&gt;limit&lt;/code&gt;, or &lt;code&gt;offset&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It won’t always break, but can produce unexpected SQL or DB errors.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best practices for &lt;code&gt;.merge&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Use &lt;code&gt;.merge&lt;/code&gt; &lt;strong&gt;only when the relation is already loaded&lt;/strong&gt; (&lt;code&gt;joins&lt;/code&gt;, &lt;code&gt;includes&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;  Avoid &lt;code&gt;select&lt;/code&gt;, &lt;code&gt;group&lt;/code&gt;, &lt;code&gt;limit&lt;/code&gt;, or &lt;code&gt;offset&lt;/code&gt; in scopes you plan to merge.&lt;/li&gt;
&lt;li&gt;  Don’t merge scopes that don’t apply to the current base model.&lt;/li&gt;
&lt;li&gt;  Name your scopes well so they’re expressive (&lt;code&gt;.visible_to_user&lt;/code&gt;, &lt;code&gt;.eligible_for_export&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  So… how should you think about &lt;code&gt;.merge&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;This is how I see it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Take this logic that’s already defined in another model and blend it into my current query… without breaking anything.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And when that’s what you need, &lt;code&gt;.merge&lt;/code&gt; is just 🔥.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>activerecord</category>
      <category>scopes</category>
    </item>
    <item>
      <title>Accelerate Maven Application Builds: Maximizing Efficiency with Docker Volumes for Maven Repository Sharing</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Sun, 19 May 2024 20:22:30 +0000</pubDate>
      <link>https://dev.to/montells/accelerate-maven-application-builds-maximizing-efficiency-with-docker-volumes-for-maven-repository-sharing-5cbn</link>
      <guid>https://dev.to/montells/accelerate-maven-application-builds-maximizing-efficiency-with-docker-volumes-for-maven-repository-sharing-5cbn</guid>
      <description>&lt;p&gt;In today's software development landscape, dockerization has become a fundamental requirement for every project. It eliminates the notorious "it works on my laptop" dilemma, promotes seamless collaboration within teams, and enables hassle-free deployment across various environments using tools like Docker Compose. Our goal is to achieve all of this while maintaining an easy-to-use and highly efficient development environment.&lt;/p&gt;

&lt;p&gt;Now, let's dive into dockerizing a Java Maven application and explore the challenges we face. Traditionally, developers often rely on using a Maven image as the base image for their Docker setup, such as the popular Maven 3.6.0 with JDK 8 on Alpine Linux:&lt;/p&gt;

&lt;p&gt;Then we want to dockerize a Java Maven application.&lt;/p&gt;

&lt;p&gt;We should to be able of &lt;/p&gt;

&lt;p&gt;build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t my-java-app .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run my-java-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the prevalent approaches is to have a maven image as a docker base image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM maven:3.6.0-jdk-8-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;include the pom&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY pom.xml .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;include the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY src ./src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;build the app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this approach poses a significant drawback. Every time we execute the docker build command, Maven will re-download the project's dependencies and rebuild the application from scratch if there are any changes in the dependency list or the source code within the src directory. This process is not only inefficient but also tedious and painfully slow.&lt;/p&gt;

&lt;p&gt;But fear not! There's a better way to handle this.&lt;/p&gt;

&lt;p&gt;By executing the dependency downloads inside the container and leveraging the power of Docker volumes, we can share the Maven repository between the host and the container. Here's how we can achieve it:&lt;/p&gt;

&lt;h2&gt;
  
  
  We can do it better.
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Execute the download dependencies inside the container and share with&lt;br&gt;
docker volume the maven repository between host and container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
Yes, Inherit from maven
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM maven:3.6.0-jdk-8-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Copy pom and code
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY pom.xml .
COPY src ./src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Use docker entrypoint.sh pattern
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY docker-entrypoint.sh /docker-entrypoint.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: this file must be created first in the root of the app and must have execution permissions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Code the docker-entrypoint.sh file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash -e
mvn package
exec $@
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Include it as a docker entrypoint
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENTRYPOINT ["/docker-entrypoint.sh"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By following this approach, we can significantly improve the efficiency of our Docker builds. The Maven dependencies will be downloaded and stored within the Docker volume, eliminating the need for repetitive downloads. Consequently, subsequent builds will be faster, more streamlined, and less error-prone.&lt;/p&gt;

&lt;p&gt;Embracing Docker not only simplifies the development and deployment process but also ensures consistent and reliable results across different environments. With these techniques in place, you can confidently build, run, and maintain your Java Maven application within a Dockerized environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Following whole example step by step
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create app
&lt;/h3&gt;

&lt;p&gt;run $:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn archetype:generate -DgroupId=dev.example -DartifactId=maven-docker-article -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Update pom.xml
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;project  xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&amp;gt;
  &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
  &amp;lt;groupId&amp;gt;dev.example&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;maven-docker-article&amp;lt;/artifactId&amp;gt;
  &amp;lt;packaging&amp;gt;jar&amp;lt;/packaging&amp;gt;
  &amp;lt;version&amp;gt;1.0-SNAPSHOT&amp;lt;/version&amp;gt;
  &amp;lt;name&amp;gt;maven-docker-article&amp;lt;/name&amp;gt;
  &amp;lt;url&amp;gt;http://maven.apache.org&amp;lt;/url&amp;gt;
  &amp;lt;properties&amp;gt;
    &amp;lt;project.build.sourceEncoding&amp;gt;UTF-8&amp;lt;/project.build.sourceEncoding&amp;gt;
    &amp;lt;maven.compiler.source&amp;gt;1.8&amp;lt;/maven.compiler.source&amp;gt;
    &amp;lt;maven.compiler.target&amp;gt;1.8&amp;lt;/maven.compiler.target&amp;gt;
  &amp;lt;/properties&amp;gt;
  &amp;lt;build&amp;gt;
    &amp;lt;plugins&amp;gt;
      &amp;lt;plugin&amp;gt;
        &amp;lt;groupId&amp;gt;org.apache.maven.plugins&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;maven-jar-plugin&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;3.2.0&amp;lt;/version&amp;gt;
        &amp;lt;configuration&amp;gt;
          &amp;lt;archive&amp;gt;
            &amp;lt;manifest&amp;gt;
              &amp;lt;mainClass&amp;gt;dev.example.App&amp;lt;/mainClass&amp;gt;
            &amp;lt;/manifest&amp;gt;
          &amp;lt;/archive&amp;gt;
        &amp;lt;/configuration&amp;gt;
      &amp;lt;/plugin&amp;gt;
    &amp;lt;/plugins&amp;gt;
  &amp;lt;/build&amp;gt;
  &amp;lt;dependencies&amp;gt;
    &amp;lt;dependency&amp;gt;
      &amp;lt;groupId&amp;gt;junit&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;junit&amp;lt;/artifactId&amp;gt;
      &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
      &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
    &amp;lt;/dependency&amp;gt;
  &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add some code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package dev.example;
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "This app works and build fast using docker entrypoint and m2 repository volumne sharing" );
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Dockerfile
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM maven:3.6.0-jdk-11 AS build

WORKDIR /app

COPY pom.xml .
COPY src ./src

COPY docker-entrypoint.sh /docker-entrypoint.sh

ENTRYPOINT ["/docker-entrypoint.sh"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create docker-entrypoint.sh file.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash -e
env
mvn package
exec $@
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add execution permissions
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;chmod +x docker-entrypoint.sh&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Build the image
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t my-maven-java-app .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Execute the container/application
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --rm -v maven-cache:/root/.m2 maven-dev-article "java -jar /app/target/maven-docker-article-1.0-SNAPSHOT.jar"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this command we are executing the java application inside its container and leveraging the maven repository shared by volume our docker volume &lt;strong&gt;maven-cache&lt;/strong&gt; and the container maven repository &lt;strong&gt;/root/.m2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From now on you docker have a volume named &lt;strong&gt;maven-cache&lt;/strong&gt; that can be used for whatever other maven app you want to dockerize using this same pattern.&lt;/p&gt;

</description>
      <category>java</category>
      <category>maven</category>
      <category>docker</category>
    </item>
    <item>
      <title>Unleashing Performance: Silencing Searchkick for Record Updates in Ruby on Rails</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Thu, 11 Jan 2024 04:23:09 +0000</pubDate>
      <link>https://dev.to/montells/how-to-avoid-elasticsearch-reindex-3999</link>
      <guid>https://dev.to/montells/how-to-avoid-elasticsearch-reindex-3999</guid>
      <description>&lt;p&gt;You have your &lt;strong&gt;model&lt;/strong&gt; configured for being indexed and searchable with &lt;strong&gt;searchkick&lt;/strong&gt; but sometimes you want to &lt;strong&gt;update&lt;/strong&gt; a record or many records or a bunch of records &lt;strong&gt;without triggering searchkick reindex&lt;/strong&gt;. It is here...&lt;/p&gt;

&lt;h2&gt;
  
  
  INTRODUCTION
&lt;/h2&gt;

&lt;p&gt;Many articles have been written about how to combine rails, elasticsearch, sidekiq, and redis because this is an almost obvious schema in Ruby on Rails applications.&lt;/p&gt;

&lt;p&gt;When you want to have a model indexed and searchable with elastisearch you go mainly with the gem searchkick.&lt;/p&gt;

&lt;p&gt;The basic, most used and first option suggested by documentation is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; class Customer &amp;lt; ApplicationRecord
   searchkick searchable: %i[name lastname],
              filterable: %i[name],
              callbacks: :async

   def search_data
     {
       id: id,
       name: name,
       lastname: lastname
      }
   end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is supposed also you have sidekiq on the table and the environment variable REDIS_URL.&lt;/p&gt;

&lt;p&gt;In this scenario &lt;/p&gt;

&lt;p&gt;When you update a customer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Customer.first.update(name: 'Jack')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then a sidekiq job is triggered for reindex this customer.&lt;/p&gt;

&lt;h2&gt;
  
  
  PROBLEM
&lt;/h2&gt;

&lt;p&gt;When you update all customers having 30 000 records:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Customer.all.each_with_index{|customer, index| customer.update(name: "Jack-#{index}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then 30 000 sidekiq jobs are triggered and enqueued for reindex all these customers&lt;/p&gt;

&lt;h2&gt;
  
  
  SOLUTION
&lt;/h2&gt;

&lt;p&gt;You can update your bunch of customers silencing searchkick and once all updates are executed you can reindex all those customers.&lt;br&gt;
Take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Searchkick.callbacks(false) do
  Customer.all.each_with_index{|customer, index| customer.update(name: "Jack-#{index}")
end
Customer.reindex(mode: :async)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Searchkick.disable_callbacks
Customer.all.each_with_index{|customer, index| customer.update(name: "Jack-#{index}")
Searchkick.enable_callbacks
Customer.reindex(mode: :async)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, once all customers are updated then some sidekiq jobs are being triggered each of them indexing a bunch of customers in a bulk giving you a huge improvement regarding performance and resources.&lt;/p&gt;

&lt;p&gt;The second way is especially useful for test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe '#has_vacancies?' do
    subject(:event) { create :event, vacancies: 0 }

    before do
      Searchkick.disable_callbacks
    end

    after do
      Searchkick.enable_callbacks
    end

    context 'when vacancies lower than 1' do
      it 'return false' do
        expect(event.vacancies?).to eq false
      end
    end

    context 'when vacancies greater than 0' do
      it 'return true' do
        lesson.update(vacancies: 1)
        expect(event.vacancies?).to eq true
      end
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>rails</category>
      <category>elasticsearch</category>
      <category>performance</category>
    </item>
    <item>
      <title>Parametrize Your Custom Bitbucket Pipeline With Variables</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Sat, 30 Dec 2023 14:47:52 +0000</pubDate>
      <link>https://dev.to/montells/parametrize-your-custom-bitbucket-pipeline-with-variables-hdp</link>
      <guid>https://dev.to/montells/parametrize-your-custom-bitbucket-pipeline-with-variables-hdp</guid>
      <description>&lt;p&gt;Bitbucket Pipelines provides a seamless integration for continuous integration and deployment (CI/CD) processes. &lt;/p&gt;

&lt;p&gt;In this article, we'll go through the process of setting up a basic custom pipeline, &lt;strong&gt;using variables&lt;/strong&gt;, and configuring &lt;strong&gt;default values&lt;/strong&gt; for a smoother and more personalized workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  USE CASE
&lt;/h2&gt;

&lt;p&gt;Sometimes we want to automate some amount of steps. Those steps will execute steps with different values.&lt;/p&gt;

&lt;p&gt;One example I faced was to &lt;a href="https://dev.to/montells/upgrade-postgres-when-using-docker-compose-4di4"&gt;build automation for upgrading Postgres version&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The process consists of some regular steps. And the bitbucket pipeline may look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipelines:
  custom:
    - upgrade_postgres:
      - step:
        name: "some saving of old version"
        script:
          - ./dump_10.sh
      - step: 
        name: "Some steps for raising up new version"
        script:
          - ./load_11.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The process consists of some regular steps that only differ on the version I want to upgrade from &lt;strong&gt;(10)&lt;/strong&gt; and the version I want to upgrade to &lt;strong&gt;(11)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of changing the bitbucket-pipeline file, for every iteration of upgrading version, we can &lt;strong&gt;use variables&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipelines:
  custom:
    - variables: #list variable names under here
       - name: UP_PG_FROM
         default: '11'
         allowed-values:
           - '11'
           - '12'
           - '13'
           - '14'
       - name: UP_PG_TO
         default: '12'
         allowed-values:
           - '12'
           - '13'
           - '14'
           - '15'   
    - upgrade_postgres:
      - step:
        name: "some saving of old version"
        script:
          - ./build_${UP_PG_FROM}.sh
      - step: 
        name: "Some steps for raising up new version"
        script:
          - ./build_${UP_PG_TO}.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that in this snipped we set up variables with a set of possible values using keyword &lt;strong&gt;allowed-values&lt;/strong&gt; and default values with keyword &lt;strong&gt;default&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And we use those values with the syntax &lt;code&gt;${variable name}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This example could lead you to this once you click on &lt;strong&gt;Run Pipeline&lt;/strong&gt;:&lt;/p&gt;

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

</description>
      <category>bitbucket</category>
      <category>cicd</category>
      <category>bitbucketpipeline</category>
      <category>ci</category>
    </item>
    <item>
      <title>Exploring the Power of Keyword Arguments in Ruby</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Sun, 26 Nov 2023 19:01:36 +0000</pubDate>
      <link>https://dev.to/montells/exploring-the-power-of-keyword-arguments-in-ruby-1gk8</link>
      <guid>https://dev.to/montells/exploring-the-power-of-keyword-arguments-in-ruby-1gk8</guid>
      <description>&lt;p&gt;Keyword Arguments was introduced in Ruby 2.0. &lt;/p&gt;

&lt;p&gt;This seems a very nice and attractive feature. &lt;/p&gt;

&lt;p&gt;In a few words and code examples I will delve into &lt;strong&gt;keyword arguments&lt;/strong&gt; in Ruby based on my own experience, opinion, and how I understand it.&lt;/p&gt;

&lt;p&gt;This technique eliminates the &lt;strong&gt;Dependency on Parameter Order&lt;/strong&gt; and &lt;strong&gt;Provide More Flexibility for Default Values.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;🚥&lt;/p&gt;

&lt;h2&gt;
  
  
  Eliminating the Dependency on Parameter Order:
&lt;/h2&gt;

&lt;p&gt;When using positional arguments, developers must know/remember the order in which the parameters are defined in the method definition. This can lead to confusion and errors, especially when methods have a large number of parameters. Keyword arguments eliminate this dependency by allowing developers to pass arguments by name instead of position. Let's consider an example:&lt;/p&gt;

&lt;h3&gt;
  
  
  With positional(traditional) parameters:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! You are &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old."&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Montells"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="no"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Montells'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="no"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the developer must know the order of the parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  With hash options:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! You are &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:age&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old."&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'Montells'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="s1"&gt;'44'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="no"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nf"&gt;unordered_person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="s1"&gt;'44'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'Montells'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unordered_person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="no"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the dependency of order parameters is eliminated however developer needs to deal with hash.&lt;/p&gt;

&lt;h3&gt;
  
  
  With Keyword Arguments:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;! You are &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old."&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Montells"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="no"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Montells"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="no"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the developer must to know the name of each parameter but not the order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ease of Providing Default Values:
&lt;/h2&gt;

&lt;p&gt;Keyword arguments also provide a convenient way to assign default values to parameters, regardless of their order. This is particularly useful when some parameters have default values and others do not. Let's illustrate this with an example:&lt;/p&gt;

&lt;h3&gt;
  
  
  With positional(traditional) parameters:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Unknown'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Name: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Age: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Country: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Montells"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Cuba"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# I want to omit age&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Cuba&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Unknown&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  With Keyword Arguments:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;country: &lt;/span&gt;&lt;span class="s1"&gt;'Unknown'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"User: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Age: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Country: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Montells"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;country: &lt;/span&gt;&lt;span class="s1"&gt;'Cuba'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Cuba&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first example, using positional arguments, we need to remember the order of the default parameters. However, in the second example, with keyword arguments, we can explicitly provide values only for the parameters we want to override, while the rest use their default values. This makes the code more flexible and less error-prone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus Track
&lt;/h3&gt;

&lt;p&gt;Some similarity can be detected between &lt;code&gt;hash&lt;/code&gt; and &lt;code&gt;keyword arguments&lt;/code&gt;. Sometimes you have a hash options like &lt;code&gt;{name: "Montells", country: 'Cuba'}&lt;/code&gt;. It can be passed to a method declared with keyword arguments using the double splat operator &lt;code&gt;**&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;country: &lt;/span&gt;&lt;span class="s1"&gt;'Unknown'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"User: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Age: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Country: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;hash_options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Montells"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;country: &lt;/span&gt;&lt;span class="s1"&gt;'Cuba'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hash_options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Montells&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Cuba&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🏁&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
Keyword arguments in Ruby, introduced in version 2.0, offer significant advantages over traditional positional arguments. They eliminate the need to remember the order of parameters, making code more readable and self-explanatory. Additionally, keyword arguments provide a convenient way to assign default values to parameters, regardless of their order. By leveraging the power of keyword arguments, Ruby developers can write more expressive, flexible, and maintainable code.&lt;/p&gt;

&lt;p&gt;Incorporating keyword arguments into your Ruby code can greatly enhance its readability and maintainability. Embrace this powerful feature and unlock the full potential of your Ruby programming skills. Happy coding!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails Models Files Organization</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Fri, 03 Nov 2023 14:40:49 +0000</pubDate>
      <link>https://dev.to/montells/rails-models-files-organization-5h7e</link>
      <guid>https://dev.to/montells/rails-models-files-organization-5h7e</guid>
      <description>&lt;p&gt;Just a place where to come back in the feature when I forget how to organize my Rails Models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyModel &amp;lt; ActiveRecord::Base
  # extends 
  # includes 
  # relationships 
  # validations 
  # callbacks 
  # scopes 
  # delegates
  # additional config (i.e. accepts_nested_attribute_for etc...) 

  # class methods 
  # public instance methods 
  # protected instance methods 
  # private instance methods 
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is worth mentioning or thanks to this project &lt;a href="https://github.com/hopsoft/rails_standards" rel="noopener noreferrer"&gt;rails_standards&lt;/a&gt;&lt;br&gt;
The place where I found this guide.&lt;/p&gt;

</description>
      <category>rails</category>
    </item>
    <item>
      <title>Enhancing Dockerfile Flexibility: The Power of ARG Directives</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Fri, 03 Nov 2023 03:40:14 +0000</pubDate>
      <link>https://dev.to/montells/docker-args-1ael</link>
      <guid>https://dev.to/montells/docker-args-1ael</guid>
      <description>&lt;h3&gt;
  
  
  ARG Directives: Enhancing Flexibility and Reducing Code Duplication
&lt;/h3&gt;

&lt;p&gt;Docker is a powerful tool for containerization and deployment of applications. When creating Docker images, it's common to encounter scenarios where certain values need to be dynamic or configurable. &lt;/p&gt;

&lt;p&gt;In this article, we'll explore how to improve the Dockerfile by leveraging ARG directives to make the Alpine version dynamic and avoid code duplication.&lt;/p&gt;

&lt;p&gt;Let's start with the initial Dockerfile:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM alpine:3.18 as base

RUN echo "Alpine version 3.18"
RUN echo "Alpine version 3.18" &amp;gt; message.txt
CMD cat message.txt

FROM base as production

RUN echo "Alpine version 3.18"
RUN echo "Alpine version 3.18" &amp;gt; message.txt
CMD cat message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;build and run&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$&amp;gt; docker build -t base --target base .
[base 2/3] RUN echo "Alpine version 3.18"
[base 3/3] RUN echo "Apline version 3.18" &amp;gt; message.txt     
$&amp;gt; docker run base
Apline version 3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we do similar build an run with target production then&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$&amp;gt; docker build -t production --target production .
[production 1/2] RUN echo "Alpine version 3.18" 
[production 2/2] RUN echo "Apline version 3.18" &amp;gt; message.txt
$&amp;gt; docker run production
Apline version 3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above Dockerfile, the Alpine version is hard-coded as 3.18, which leads to code duplication and lack of flexibility. We can improve this step by step using ARG directives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Dynamic Alpine Version in the FROM Directive
&lt;/h3&gt;

&lt;p&gt;We can use the ARG directive to define an argument that can be expanded during the build process. Let's modify the first two lines as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG ALPINE_VERSION
FROM alpine:${ALPINE_VERSION} as base
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, during the Docker build command, we can pass the value for the ALPINE_VERSION argument using the --build-arg flag:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t base --target base --build-arg ALPINE_VERSION=3.18 .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This allows us to have a more flexible image where the Alpine version can be configured at build time.    &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Default Value for Dynamic Alpine Version
&lt;/h3&gt;

&lt;p&gt;We can provide a default value for the ALPINE_VERSION argument by modifying the first line as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG ALPINE_VERSION='3.18'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, if the ALPINE_VERSION argument is not provided during the Docker build command, the default value will be used:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t base --target base .
docker run base
Apline version 3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice that the value 3.18 is used even when was not provided in the docker build command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$&amp;gt; docker build -t base --target base --build-arg ALPINE_VERSION=3.17 .
[internal] load metadata for docker.io/library/alpine:3.17
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our build process use the value provided in the docker build command using the --build-arg flag&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Using the Same Value Inside a Build Stage
&lt;/h3&gt;

&lt;p&gt;In the original Dockerfile, the ARG ALPINE_VERSION was not expanded inside the build stage. To make it accessible, we need to duplicate its definition inside the build stage. Modify the Dockerfile as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG ALPINE_VERSION='3.18'
FROM alpine:${ALPINE_VERSION} as base

ARG ALPINE_VERSION
RUN echo "Alpine version ${ALPINE_VERSION}"
RUN echo "Alpine version ${ALPINE_VERSION}" &amp;gt; message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, the ARG &lt;strong&gt;ALPINE_VERSION&lt;/strong&gt; is accessible inside the build stage, and the default value is also available:    &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$&amp;gt; docker build -t base --target base .
[base 2/3] RUN echo "Alpine version 3.18"
[base 3/3] RUN echo "Apline version 3.18" &amp;gt; message.txt 
$&amp;gt; docker run base
Apline version 3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;IMPORTANT: The line &lt;em&gt;ARG ALPINE_VERSION&lt;/em&gt; . The ARG must be re-defined(duplicated) inside the build stage &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 4: Using the Same Value Inside Different Build Stages
&lt;/h3&gt;

&lt;h4&gt;
  
  
  When Inherits
&lt;/h4&gt;

&lt;p&gt;If build stages inherit from each other, they can access the arguments defined in their parent stage. Modify the Dockerfile as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG ALPINE_VERSION='3.18'

FROM alpine:${ALPINE_VERSION} as base
ARG ALPINE_VERSION
RUN echo "Alpine version ${ALPINE_VERSION}"
RUN echo "Alpine version ${ALPINE_VERSION}" &amp;gt; message.txt
CMD cat message.txt

FROM base as production
RUN echo "Alpine version ${ALPINE_VERSION}"
RUN echo "Alpine version ${ALPINE_VERSION}" &amp;gt; message.txt
CMD cat message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this case, the build stage "production" inherits from "base," so it can access the arguments defined in the "base" stage without duplicating their definitions. The same value of ALPINE_VERSION will be used in both stages.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$&amp;gt; docker build -t production --target production .
=&amp;gt; CACHED [base 2/3] RUN echo "Alpine version 3.18"
=&amp;gt; CACHED [base 3/3] RUN echo "Apline version 3.18" &amp;gt; message.txt
=&amp;gt; [production 1/2] RUN echo "Alpine version 3.18" 
=&amp;gt; [production 2/2] RUN echo "Apline version 3.18" &amp;gt; message.txt
$&amp;gt; docker run production
Apline version 3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  When no Inherits
&lt;/h4&gt;

&lt;p&gt;If a build stage does not inherit from another stage, the arguments need to be duplicated inside that stage for access.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG  ALPINE_VERSION='3.18'

FROM  alpine:${ALPINE_VERSION}  as  base
ARG  ALPINE_VERSION
RUN  echo  "Alpine  version  ${ALPINE_VERSION}"
RUN  echo  "Apline  version  ${ALPINE_VERSION}"  &amp;gt;  message.txt
CMD  cat  message.txt

FROM  alpine:${ALPINE_VERSION}  as  staging
ARG  ALPINE_VERSION
RUN  echo  "Alpine  version  ${ALPINE_VERSION}"
RUN  echo  "Apline  version  ${ALPINE_VERSION}"  &amp;gt;  message.txt
CMD  cat  message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;ouput:  &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$&amp;gt; docker build -t staging --target staging .
=&amp;gt;[staging 3/3] RUN echo "Apline version 3.18"
&amp;gt; message.txt
$&amp;gt; docker run staging
Apline version 3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice: re-duplicate ARG ALPINE_VERSION&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;By using ARG directives, we can avoid code duplication and build more flexible Docker images. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ARGs defined outside build stages (before FROM) are accessible only in the lines with the FROM directive. &lt;/li&gt;
&lt;li&gt;ARGs defined inside a build stage are accessible only within that stage and its child stages. &lt;/li&gt;
&lt;li&gt;To access the value of an ARG defined outside from inside a build stage, duplicate its definition in the build stage.
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>docker</category>
    </item>
    <item>
      <title>Dealing with openssl when install old Ruby in recent Ubuntu</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Fri, 15 Sep 2023 17:49:23 +0000</pubDate>
      <link>https://dev.to/montells/install-old-ruby-in-recent-ubuntu-3ink</link>
      <guid>https://dev.to/montells/install-old-ruby-in-recent-ubuntu-3ink</guid>
      <description>&lt;p&gt;Many of you must have faced the problem of installing an old version of Ruby on a new Ubuntu.&lt;/p&gt;

&lt;p&gt;Several threads throughout the internet demonstrate this, and various solutions have been shared.&lt;/p&gt;

&lt;p&gt;The most common issue is the incompatibility with the OpenSSL version.&lt;/p&gt;

&lt;p&gt;Among the most daunting solutions for non-expert Linux users is "compiling OpenSSL, installing it, and configuring Ruby to use this version." At least for me, that option is quite intimidating.&lt;/p&gt;

&lt;p&gt;However, &lt;a href="https://rvm.io/" rel="noopener noreferrer"&gt;RVM&lt;/a&gt; provides a native solution.&lt;/p&gt;

&lt;p&gt;ex: In ubuntu 22.04&lt;br&gt;
trying to install ruby 2.7.4 like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rvm install 2.7.4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;will raise errors similar to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/usr/share/rvm/rubies/ruby-2.5.0/lib/libruby.so.2.5(rb_vm_bugreport+0x4ae) [0x7f03e16b18ce]
/usr/share/rvm/rubies/ruby-2.5.0/lib/libruby.so.2.5(0xaafbd) [0x7f03e152ffbd]
/usr/share/rvm/rubies/ruby-2.5.0/lib/libruby.so.2.5(0x19e2e6) [0x7f03e16232e6]
/lib/x86_64-linux-gnu/libc.so.6(0x7f03e128e520) [0x7f03e128e520]
/lib/x86_64-linux-gnu/libssl.so.3(0x7f03df83e140) [0x7f03df83e140]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution that works for me&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rvm pkg install openssl
rvm install 2.7.4 --with-openssl-dir=/usr/share/rvm/usr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this helps you.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rvm</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Things, tricks, hacks, workarounds...</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Sun, 23 Apr 2023 19:55:58 +0000</pubDate>
      <link>https://dev.to/montells/things-tricks-hacks-workarounds-f1m</link>
      <guid>https://dev.to/montells/things-tricks-hacks-workarounds-f1m</guid>
      <description>&lt;p&gt;The intention of this entry is just to group some small post which contain some tricks, hacks or solutions I found googling.&lt;/p&gt;

&lt;p&gt;I am completely sure I will forget those solutions. Then I will use &lt;a href="https://dev.to"&gt;DEV&lt;/a&gt; as memory helper.&lt;/p&gt;

&lt;p&gt;Several of this solutions will be useful also for other programmers if they happen to fall in the same situation.&lt;/p&gt;

&lt;p&gt;Then this will help me a lot and also will help someone else.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/montells/curl-able-to-resume-54ie"&gt;Command curl able to resume&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/montells/rails-models-files-organization-5h7e"&gt;Rails Models Files Organization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/montells/git-broken-reference-3faj"&gt;Git broken references&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/montells/copy-paste-postgresql-query-results-into-google-sheets-no-csv-needed-3omb"&gt;Copy from sql and paste in spreedsheet&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Curl able to resume.</title>
      <dc:creator>Michel Sánchez Montells</dc:creator>
      <pubDate>Sun, 23 Apr 2023 19:52:02 +0000</pubDate>
      <link>https://dev.to/montells/curl-able-to-resume-54ie</link>
      <guid>https://dev.to/montells/curl-able-to-resume-54ie</guid>
      <description>&lt;p&gt;If you don't want to read:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spolier&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -L -O -C - 'https://internet-address/ubuntu-22.04.2-desktop-amd64.iso'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Some context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Writing this post with the goal knowing where to find out this again in the future and maybe help someone out there.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can stay in the situation of downloading big file from internet using  &lt;strong&gt;curl&lt;/strong&gt;. And you can face the situation of bad connectivity and the downloading is interrupted continuously before ending and you need to start again.&lt;/p&gt;

&lt;p&gt;Then you would like to be able of resume from the last byte.&lt;/p&gt;

&lt;p&gt;The default or first curl command will looks like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;curl command &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl --header 'Host: mirrors.ircam.fr'
     --header 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' 
     --header 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' 
     --header 'Accept-Language: es-CU,es;q=0.9,en-US;q=0.8,en;q=0.7,es-419;q=0.6' 
     --header 'Referer: https://ubuntu.com/' 
     'https://mirrors.ircam.fr/pub/ubuntu/releases/22.04.2/ubuntu-22.04.2-desktop-amd64.iso' 
      -L -o 'ubuntu-22.04.2-desktop-amd64.iso'
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This command will be downloading with file name &lt;strong&gt;ubuntu-22.04.2-desktop-amd64.iso&lt;/strong&gt; however once the connection get closed after 600Mb of 3Gb if you retry the same command the downloading will start from 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;curl command &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl --header 'Host: mirrors.ircam.fr' 
     --header 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' 
     --header 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' 
     --header 'Accept-Language: es-CU,es;q=0.9,en-US;q=0.8,en;q=0.7,es-419;q=0.6' 
     --header 'Referer: https://ubuntu.com/' 
     -L -O -C - 'https://mirrors.ircam.fr/pub/ubuntu/releases/22.04.2/ubuntu-22.04.2-desktop-amd64.iso'`
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The parameter &lt;code&gt;-C&lt;/code&gt; will ensure that the file is downloading like resuming, starting with the proper offset.&lt;br&gt;
The parameter &lt;code&gt;-O&lt;/code&gt; will assing to the downloaded local file name the same name of the remote file.&lt;/p&gt;

</description>
      <category>linux</category>
    </item>
  </channel>
</rss>
