<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>All Posts - MC Squared</title><link>https://mc-squared.dev/posts/</link><description>All Posts | MC Squared</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Thu, 30 Jun 2022 08:59:20 +1200</lastBuildDate><atom:link href="https://mc-squared.dev/posts/" rel="self" type="application/rss+xml"/><item><title>Rails' Hidden Gems: ActiveRecord Store</title><link>https://mc-squared.dev/2022/06/hidden-gems-activerecord-store/</link><pubDate>Thu, 30 Jun 2022 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2022/06/hidden-gems-activerecord-store/</guid><description><![CDATA[<p>Rails is a large framework with a lot of handy built-in tools for specific situations. In this series, we&rsquo;ll take a look at some of the lesser-known tools hidden in Rails&rsquo; large codebase.</p>
<p>In this article, we&rsquo;ll focus on ActiveRecord&rsquo;s <code>store</code> and <code>store_accessor</code> methods. Both of these methods are aimed at the use case of storing structured data in a database column, such as JSON or YAML. While <code>store_accessor</code> gives us a handy way to grab values from these data without clogging up a model with getter methods, <code>store</code> goes a step further and transparently serializes/deserializes data to our chosen format. To understand where this can be useful, we&rsquo;ll also take a look at options for storing JSON in relational databases and some of the reasons you may want to do so.</p>]]></description></item><item><title>Rails' Hidden Gems: ActiveSupport Cache Increment and Decrement</title><link>https://mc-squared.dev/2022/06/hidden-gems-active-support-cache-increment/</link><pubDate>Sun, 05 Jun 2022 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2022/06/hidden-gems-active-support-cache-increment/</guid><description><![CDATA[<p>Rails is a large framework with a lot of handy tools built-in for specific situations. In this series, we&rsquo;ll take a look at some of the lesser-known tools hidden in Rails&rsquo; large codebase.</p>
<p>In this article, we&rsquo;ll explain the <code>increment</code> and <code>decrement</code> methods in <code>Rails.cache</code>.</p>
<h2 id="the-railscache-helper">The Rails.cache Helper</h2>
<p><code>Rails.cache</code> is the entryway to interact with the cache in your application. It&rsquo;s also an abstraction, giving you a common API to call regardless of the actual cache &ldquo;store&rdquo; being used under the hood. Out of the box, Rails supports the following:</p>]]></description></item><item><title>Rails' Hidden Gems: ActiveSupport's #descendants</title><link>https://mc-squared.dev/2021/11/hidden-gems-activesupport-descendants/</link><pubDate>Thu, 04 Nov 2021 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2021/11/hidden-gems-activesupport-descendants/</guid><description><![CDATA[<p>Rails adds many things to Ruby&rsquo;s built-in objects. This is what some call a &ldquo;dialect&rdquo; of Ruby and is what allows Rails developers to write lines like <code>1.day.ago</code>.</p>
<p>Most of these extra methods live in ActiveSupport. Today, we&rsquo;re going to look at perhaps a lesser-known method that ActiveSupport adds directly to Class: <code>descendants</code>. This method returns all the subclasses of the called class. For example, <code>ApplicationRecord.descendants</code> will return the classes in your app that inherit from it (e.g., all the models in your application). In this article, we&rsquo;ll take a look at how it works, why you might want to use it, and how it augments Ruby&rsquo;s built-in inheritance-related methods.</p>]]></description></item><item><title>Rails' Hidden Gems: Prevent Race Conditions with ActiveRecord's #update_counters</title><link>https://mc-squared.dev/2021/09/hidden-gems-activerecord-update-counters/</link><pubDate>Thu, 02 Sep 2021 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2021/09/hidden-gems-activerecord-update-counters/</guid><description><![CDATA[<p>Rails is a large framework with a lot of handy tools built-in for specific situations. In this series, we&rsquo;re taking a look at some of the lesser-known tools hidden in Rails&rsquo; large codebase.</p>
<p>In this article in the series, we&rsquo;re going to take a look at ActiveRecord&rsquo;s <code>update_counters</code> method. In the process, we&rsquo;ll look at the common trap of &ldquo;race conditions&rdquo; in multithreaded programs and how this method can prevent them.</p>]]></description></item><item><title>Rails' Hidden Gems: ActiveSupport StringInquirer</title><link>https://mc-squared.dev/2021/05/hidden-gems-activesupport-stringinquirer/</link><pubDate>Wed, 19 May 2021 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2021/05/hidden-gems-activesupport-stringinquirer/</guid><description><![CDATA[<p>Rails is a large framework, and it grows larger every year. This makes it easy for some helpful features to slip by unnoticed. In this series, we&rsquo;ll take a look at some lesser-known functionality built into Rails for specific tasks.</p>
<p>In the first article in this series, we&rsquo;re going to take a look at how calling <code>Rails.env.test?</code> actually works under-the-hood by using the little-known <code>StringInquirer</code> class from ActiveSupport. Going one step further, we will also walk through the <code>StringInquirer</code> source code to see how it works, which (spoiler alert) is a simple example of Ruby&rsquo;s metaprogramming via the special <code>method_missing</code> method.</p>]]></description></item><item><title>HTTP Caching in Ruby on Rails Applications</title><link>https://mc-squared.dev/2021/05/http-caching-in-ruby-on-rails-applications/</link><pubDate>Wed, 05 May 2021 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2021/05/http-caching-in-ruby-on-rails-applications/</guid><description>&lt;p>A general way to describe caching is storing some data so that we can quickly retrieve it later. Sometimes, this means storing computed data so that it does not need to be re-computed, but it can also refer to storing data locally to avoid having to fetch it again. Your computer does this constantly, as your operating system tries to keep frequently accessed data in RAM so that it doesn&amp;rsquo;t have to be fetched again from a hard drive or SSD.&lt;/p></description></item><item><title>How ActiveRecord Uses Caching To Avoid Unnecessary Trips To The Database</title><link>https://mc-squared.dev/2021/02/how-active-record-caches-by-default-in-ruby-on-rails/</link><pubDate>Sat, 20 Feb 2021 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2021/02/how-active-record-caches-by-default-in-ruby-on-rails/</guid><description>&lt;p>A general way to describe caching is storing the result of some code so that we can quickly retrieve it later. In some cases, this means storing a computed value to avoid needing to recompute it later. However, we can also cache data by simply keeping it in memory, without performing any computations, to avoid having to read from a hard drive or perform a network request.&lt;/p>
&lt;p>This latter form is particularly relevant for ActiveRecord, where the database often runs on a separate server. Thus, all requests incur network-traffic overhead, not to mention the load placed on the database server when the query is performed again.&lt;/p></description></item><item><title>Everything You Ever Wanted To Know About View Caching In Rails</title><link>https://mc-squared.dev/2020/11/caching-views-in-ruby-on-rails-applications/</link><pubDate>Fri, 13 Nov 2020 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/11/caching-views-in-ruby-on-rails-applications/</guid><description>&lt;p>Caching is a general term that means storing the result of some code so that we can quickly retrieve it later. This allows us to, for example, avoid hitting the database over and over to get data that rarely changes. Although the general concept is the same for all types of caching, Rails provides us with different aids depending on what we are trying to cache.&lt;/p>
&lt;p>For Rails developers, common forms of caching include memoization, low-level caching (both covered in previous parts of this caching series), and view caching, which we will cover here.&lt;/p></description></item><item><title>Using Low-Level Caching in Ruby on Rails Applications</title><link>https://mc-squared.dev/2020/10/using-low-level-caching-in-ruby-on-rails-applications/</link><pubDate>Thu, 01 Oct 2020 08:59:20 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/10/using-low-level-caching-in-ruby-on-rails-applications/</guid><description>&lt;h2 id="types-of-caching">Types of Caching&lt;/h2>
&lt;p>Caching is a general term which means storing the result of some code so that we can quickly retrieve it later. This allows us to, for example, perform some heavy number-crunching once and then just re-use the value without having to recalculate it again. Although the general concept is the same for all types of caching, there are various mechanisms we can use depending on what we are trying to cache.&lt;/p></description></item><item><title>Build a Code Library</title><link>https://mc-squared.dev/2020/08/build-a-code-library/</link><pubDate>Wed, 26 Aug 2020 09:00:00 +1200</pubDate><author>Jonathan Miles</author><guid>https://mc-squared.dev/2020/08/build-a-code-library/</guid><description><![CDATA[<p>It&rsquo;s no secret that <strong>developers who create toy apps, side projects, or tutorials, benefit from the process</strong>. Initially, I thought this could be boiled down to just being good forms of learning. Building something new, and even more so teaching something new, are both great ways to learn.</p>
<p>There is another benefit, however: Developers who do this are <strong>building their own code library</strong>. Sure, maybe the code isn&rsquo;t production-ready, but it&rsquo;s at least a (hopefully) working example that can act as a combination of reference implementation, quickstart guide, and memory-jogger.</p>]]></description></item></channel></rss>