<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Allan Monge on Medium]]></title>
        <description><![CDATA[Stories by Allan Monge on Medium]]></description>
        <link>https://medium.com/@allanmongej?source=rss-8fbae0468aec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*n-dL4n-x7wj-KgIx.jpeg</url>
            <title>Stories by Allan Monge on Medium</title>
            <link>https://medium.com/@allanmongej?source=rss-8fbae0468aec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 06:43:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@allanmongej/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RoR Caching ActiveRecord queries]]></title>
            <link>https://medium.com/@allanmongej/ror-caching-activerecord-queries-27f892320b20?source=rss-8fbae0468aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/27f892320b20</guid>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[activerecord]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Allan Monge]]></dc:creator>
            <pubDate>Thu, 23 Apr 2020 21:25:17 GMT</pubDate>
            <atom:updated>2020-04-23T21:27:39.537Z</atom:updated>
            <content:encoded><![CDATA[<h3>Ruby on Rails — Caching ActiveRecord queries</h3><p>In order to speed up your app it is a common technique to cache queries using Rails.cache.fetch(key), but there are some important issues to take into account.</p><h3>ActiveRecord::Relation VS Query result</h3><p>If you cache queries like the following piece of code, Rails is actually caching a scope so if you try a second attempt of fetch Rails.cache.fetch(key) the query will be executed again. No fun at all.</p><pre>Rails.cache.fetch(key) do<br>  Dog.where(age: 9).limit(3)<br>end</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S4EyaXgDjITBoI3FDxlDZg.png" /><figcaption>A second SQL query is executed</figcaption></figure><p>Instead, if you want to cache the query results append .all for Rails &lt; 4 and .load for Rails =&gt; 4 to the query, like below.</p><pre>Rails.cache.fetch(key) do<br>  Dog.where(age: 9).limit(1000).all  # Rails &lt; 4<br>  Dog.where(age: 9).limit(1000).load # Rails =&gt; 4<br>end</pre><figure><img alt="No SQL query is executed in second attempt" src="https://cdn-images-1.medium.com/max/1024/1*m1aujQA57bETgdkzBGcGrQ.png" /><figcaption>No second SQL query is executed in second attempt</figcaption></figure><h3>Caching objects</h3><p>One of the problems of using cache to store objects is that it might result in inconsistent states and values, objects can be changed or deleted. So it is a good idea to cache only primitive objects, when applicable. In this case ids.</p><pre>ids = Rails.cache.fetch(key) do<br>  Dog.where(age: 9).limit(1000).pluck(:id)<br>end</pre><pre>Dog.find(ids)</pre><p>Even though Dog.find(ids) is always executed, query using the primary key is fast and you solved the problem of inconsistent objects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27f892320b20" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up CI/CD to a Rails 6 API with CircleCI and Heroku]]></title>
            <link>https://medium.com/@allanmongej/setting-up-ci-cd-to-a-rails-6-api-with-circleci-and-heroku-c00064f31d57?source=rss-8fbae0468aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/c00064f31d57</guid>
            <category><![CDATA[circleci]]></category>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[Allan Monge]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 21:57:00 GMT</pubDate>
            <atom:updated>2020-04-23T17:43:56.430Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JbY2npcKa_vSJrH8" /><figcaption>Photo by <a href="https://unsplash.com/@angelyviviana55?utm_source=medium&amp;utm_medium=referral">Angely Acevedo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article we are going to set up continuous integration/delivery for a Ruby on Rails 6 API making use of CircleCI and Heroku.</p><h3>Technology Stack</h3><ul><li>Ruby on Rails 6</li><li>Postgres 11</li><li>Rspec</li><li>Github</li><li>CircleCi</li><li>Heroku</li></ul><h3>Important notes</h3><ul><li><em>This guide works whether or not you use Docker for development environment, in case you want to implement it please check out my tutorial → </em><a href="https://medium.com/@allanmongej/fast-guide-creating-a-dockerized-rails-api-using-postgres-5a0fc702d394"><em>https://medium.com/@allanmongej/fast-guide-creating-a-dockerized-rails-api-using-postgres-5a0fc702d394</em></a></li><li><em>It is assumed you already have a running RoR API.</em></li><li><em>It is assumed you already have a test suite using rspec, in case you use another test framework just go ahead and check out CircleCI docs.</em></li><li><em>It is assumed that you already know how to use Heroku.</em></li></ul><p>As you may know there are a lot of CI/CD tools and cloud platforms, I chose CircleCI and Heroku for the following reasons:</p><ul><li>They are amazingly easy to use.</li><li>Both have free plans (which is very important for a project in dev phase).</li><li>They are well documented.</li></ul><p>Without further ado…</p><h3>1. Rails gem dependencies</h3><p>Go ahead and add the following gem</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f5ebd27018e843b1f2042375b4a5c52/href">https://medium.com/media/5f5ebd27018e843b1f2042375b4a5c52/href</a></iframe><h3>2. Add access to Circle to your GitHub repo</h3><p>Create an account in CircleCI (If you don’t already have it), go to Projects section and allow access to your repository.</p><p>CircleCI will ask you if you want to automatically add the config file or if you prefer to do it manually, let’s do it manually.</p><h3>3. Create the CircleCI config file</h3><p>Create a folder in your project root path called <em>.circleci </em>and inside it create a file called <em>config.yml</em></p><p>I used the template shared in CircleCI documentation <a href="https://circleci.com/docs/2.0/language-ruby/">https://circleci.com/docs/2.0/language-ruby/</a> and adjusted a little bit removing the Yarn-Webpacker section since the API does not include assets.</p><p>Once you changed the values like your <strong><em>app name</em></strong> or <strong><em>postgres credentials</em></strong> just go ahead and push your new file and automatically CircleCI will start building the environment and running the test suite, so far… so good!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f31b26411494cbb69674cbc8d8a761d/href">https://medium.com/media/1f31b26411494cbb69674cbc8d8a761d/href</a></iframe><h3>4. Setup CircleCI &lt;&gt; Heroku</h3><p>Create the pipeline and respectively app then go to your account settings and get you API Key so we can allow CircleCI to connect to your Heroku account.</p><p>Then go to CircleCI, select your project and go to project settings there you will find Environment Variables, go add the following vars:</p><ul><li>HEROKU_API_KEY</li><li>HEROKU_APP_NAME</li></ul><h3>5. CircleCI Orbs</h3><p>CircleCI 2.1 introduced the concept of Orbs which basically are reusable commands, jobs and executors. Learn more → <a href="https://circleci.com/orbs/">https://circleci.com/orbs/</a></p><p>And fortunately CircleCI has one already for Heroku, so go again to your <em>.circleci/config.yml</em> file and add the following section:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e4489f2d7ee11dfafa00de17dcb3922/href">https://medium.com/media/7e4489f2d7ee11dfafa00de17dcb3922/href</a></iframe><p>And that’s it, now you can push your new config file and “automagically” in a matter of seconds you will have your application deployed into Heroku and ready to be used.</p><p>The final <em>config.yml should look like this:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbb309209d063fceb2a49348c0891def/href">https://medium.com/media/cbb309209d063fceb2a49348c0891def/href</a></iframe><h3>6. Rails migrations</h3><p>In case you want your migrations to be executed with every deployment, Heroku introduced Release Phase which as per the docs it “enables you to run certain tasks before a new release of your app is deployed”</p><p>So go ahead and add a Procfile in your project root folder with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9066a800b9c312b7b83e0bd6937497e/href">https://medium.com/media/c9066a800b9c312b7b83e0bd6937497e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c00064f31d57" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Develop local ruby gems on Docker Compose]]></title>
            <link>https://medium.com/@allanmongej/develop-local-ruby-gems-on-docker-compose-a596d0592417?source=rss-8fbae0468aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/a596d0592417</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[gem]]></category>
            <category><![CDATA[docker-compose]]></category>
            <dc:creator><![CDATA[Allan Monge]]></dc:creator>
            <pubDate>Mon, 18 Nov 2019 21:55:53 GMT</pubDate>
            <atom:updated>2019-11-18T21:55:53.707Z</atom:updated>
            <content:encoded><![CDATA[<h3>Working with local ruby gems on Docker Compose</h3><p>Let’s suppose we have setup the development environment of Rails application on Docker and this app uses a gem which you need to modify for a reason, without Docker it would be as simple as adding inside the Gemfile the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f85779c73beca6aa03310c5a5ce168ed/href">https://medium.com/media/f85779c73beca6aa03310c5a5ce168ed/href</a></iframe><p>Then you execute your changes to the gem and test them directly in your app without having to push your changes to the repo. (Please don’t use this to avoid TDD).</p><p>But if you use Docker for development you already know that containers don’t have access to your local machine so we can’t access the folder with the gem files directly.</p><h4>How to do it?</h4><p>Mounting volumes, basically we will tell Docker to mount a new volume that makes reference to a specific folder in our machine so that our app can access it.</p><ol><li>Inside your docker-compose.yml under your app configs add a volume with the path of your local machine folder</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/249e40d0cbabe953e9040e520d2b9032/href">https://medium.com/media/249e40d0cbabe953e9040e520d2b9032/href</a></iframe><p>The line #8 tells Docker to copy the folder ~/projects/gem_name in volume and place it under /app/vendor/gems/gem_name</p><p>2. So now we just add the reference in our Gemfile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/989d1d7855b0e360ed9fced41c628ace/href">https://medium.com/media/989d1d7855b0e360ed9fced41c628ace/href</a></iframe><p>Improvements to this post are always welcome.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a596d0592417" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails 6 <> Webpack<> Docker]]></title>
            <link>https://medium.com/@allanmongej/rails-6-webpack-docker-5a317ff660c5?source=rss-8fbae0468aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a317ff660c5</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[ruby-on-rails-6]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Allan Monge]]></dc:creator>
            <pubDate>Tue, 30 Jul 2019 07:21:29 GMT</pubDate>
            <atom:updated>2020-03-07T04:59:04.602Z</atom:updated>
            <content:encoded><![CDATA[<p>This is an alive (continuously updated) guide about how to accomplish tasks related to Rails 6, Webpack and Docker.</p><p>Before you start I would recommend you to read this great article → <a href="https://medium.com/statuscode/introducing-webpacker-7136d66cddfb">https://medium.com/statuscode/introducing-webpacker-7136d66cddfb</a></p><h3>1) Install Bootstrap</h3><p><a href="https://gorails.com/episodes/how-to-use-bootstrap-with-webpack-and-rails">https://gorails.com/episodes/how-to-use-bootstrap-with-webpack-and-rails</a></p><h3>2) Import images</h3><p>If you want to import images, it is completely different from the previous process using assets pipeline, now the images (and also stylesheets) should be stored inside the javascript folder inside assets.</p><p><em>Reference</em>: <a href="https://github.com/rails/webpacker/blob/master/docs/assets.md#link-in-your-rails-views">https://github.com/rails/webpacker/blob/master/docs/assets.md#link-in-your-rails-views</a></p><p><strong>2.1) Dir tree</strong></p><pre>app/javascript:<br>  - packs<br>    - app.js<br>  - images<br>    - calendar.png</pre><p><strong>2.2) Add inside app/javascript/packs/application.js</strong></p><pre>// import all image files in a folder:<br>require.context(&#39;../images&#39;, true)</pre><p><strong>2.3) And in your views Views</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88053f5e8bfa55cf360a5872911192af/href">https://medium.com/media/88053f5e8bfa55cf360a5872911192af/href</a></iframe><h3>Docker</h3><h3>Docker</h3><p>If you are using Docker, go ahead and follow these simple steps, doing these you easily accomplish live reloading→ <a href="https://github.com/rails/webpacker/blob/master/docs/docker.md">https://github.com/rails/webpacker/blob/master/docs/docker.md</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a317ff660c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Fast Guide] Creating a Dockerized Rails API using Postgres]]></title>
            <link>https://medium.com/@allanmongej/fast-guide-creating-a-dockerized-rails-api-using-postgres-5a0fc702d394?source=rss-8fbae0468aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a0fc702d394</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[compose]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Allan Monge]]></dc:creator>
            <pubDate>Thu, 13 Jun 2019 20:04:57 GMT</pubDate>
            <atom:updated>2020-03-11T20:54:24.552Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*haD0OW_ATYurtGDe0-7kcQ.jpeg" /></figure><p>In this guide I’m creating a rails API that will manage the users(clients) information of a system built using microservices architecture. It will run using Docker and Docker compose with a Postgres DB.</p><p>I’m not getting into the details of the API, just the config to get the app running.</p><p>I’ll use ruby-2.6.3 with rails framework version 6.</p><h3>1. The Setup</h3><ol><li>Docker and Docker Compose should be installed.</li><li>Create a new dir (The app’s folder!) all files that we are going to create will be stored here.</li></ol><pre>$ mkdir users_manager &amp;&amp; cd users_manager</pre><pre>$ touch Dockerfile docker-compose.yml Gemfile Gemfile.lock</pre><h3><strong>2. The Dockerfile</strong></h3><p>Dockerfile is the building block of Docker images and containers. In essence, it’s a list of commands the Docker engine runs to assemble the image, and thus instances of images as containers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69bd634721357a2ecf3aca1e65edf6ba/href">https://medium.com/media/69bd634721357a2ecf3aca1e65edf6ba/href</a></iframe><h3>3. The Gemfile &amp; Gemfile.lock</h3><p>We need to create the Gemfile and a Gemfile.lock (empty file) so that Docker can copy them and execute the <em>bundle install </em>command to install rails<em>, </em>both files will be overwritten by <em>rails new </em>command<em> </em>later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7751f0cdcb89e460a61e5f446117baf9/href">https://medium.com/media/7751f0cdcb89e460a61e5f446117baf9/href</a></iframe><h3>4. Docker compose</h3><p>Compose is a tool for defining and running multi-container Docker applications. Now we are going to create a docker-compose.yml file (same dir), these are the instructions to run the api container and db container and allow them to talk to each other.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da10bd49415d13be444528913cecc534/href">https://medium.com/media/da10bd49415d13be444528913cecc534/href</a></iframe><h3>5. Generate the App</h3><pre>$ docker-compose run web rails new . --api --force --database=postgresql --skip-bundle</pre><p>Now if you check your directory it will contain all the rails generated files.</p><p>In case your are using Linux run the following to own the files.</p><pre>$ sudo chown -R $USER:$USER .</pre><h3>6. Database config</h3><p>The important thing here is the <strong><em>host</em></strong>, it should be the same as the one configured in the docker-compose.yml file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bf1cad9197b221c621b5c73cfc5c498/href">https://medium.com/media/3bf1cad9197b221c621b5c73cfc5c498/href</a></iframe><h3>7. Build the Project</h3><p>Now it’s time to build the project. Also, it should be run every time you modify the Gemfile.</p><pre>$ docker-compose build</pre><h3>8. The rails commands</h3><p>Now all the rails commands should be prefixed by <strong><em>docker-compose run [container’s name] [rails command]</em></strong></p><pre>$ docker-compose run web rails db:setup</pre><h3>Final comments</h3><ul><li>Postgres is a docker image, thats why we didn’t have to execute bored steps to get it installed, the docker-compose config was enough to get Postgres running. Thanks Docker!</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a0fc702d394" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>