<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Hidden Gems - Tag - MC Squared</title><link>https://mc-squared.dev/tags/hidden-gems/</link><description>Hidden Gems - Tag - 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/tags/hidden-gems/" 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>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></channel></rss>