avatar

Rails' Hidden Gems: ActiveRecord Store

Rails is a large framework with a lot of handy built-in tools for specific situations. In this series, we’ll take a look at some of the lesser-known tools hidden in Rails’ large codebase. In this article, we’ll focus on ActiveRecord’s store and store_accessor 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 store_accessor gives us a handy way to grab values from these data without clogging up a model with getter methods, store goes a step further and transparently serializes/deserializes data to our chosen format.

Rails' Hidden Gems: ActiveSupport Cache Increment and Decrement

Rails is a large framework with a lot of handy tools built-in for specific situations. In this series, we’ll take a look at some of the lesser-known tools hidden in Rails’ large codebase. In this article, we’ll explain the increment and decrement methods in Rails.cache. The Rails.cache Helper Rails.cache is the entryway to interact with the cache in your application. It’s also an abstraction, giving you a common API to call regardless of the actual cache “store” being used under the hood.

Rails' Hidden Gems: ActiveSupport's #descendants

Rails adds many things to Ruby’s built-in objects. This is what some call a “dialect” of Ruby and is what allows Rails developers to write lines like 1.day.ago. Most of these extra methods live in ActiveSupport. Today, we’re going to look at perhaps a lesser-known method that ActiveSupport adds directly to Class: descendants. This method returns all the subclasses of the called class. For example, ApplicationRecord.descendants will return the classes in your app that inherit from it (e.

Rails' Hidden Gems: Prevent Race Conditions with ActiveRecord's #update_counters

Rails is a large framework with a lot of handy tools built-in for specific situations. In this series, we’re taking a look at some of the lesser-known tools hidden in Rails’ large codebase. In this article in the series, we’re going to take a look at ActiveRecord’s update_counters method. In the process, we’ll look at the common trap of “race conditions” in multithreaded programs and how this method can prevent them.

Rails' Hidden Gems: ActiveSupport StringInquirer

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’ll take a look at some lesser-known functionality built into Rails for specific tasks. In the first article in this series, we’re going to take a look at how calling Rails.env.test? actually works under-the-hood by using the little-known StringInquirer class from ActiveSupport. Going one step further, we will also walk through the StringInquirer source code to see how it works, which (spoiler alert) is a simple example of Ruby’s metaprogramming via the special method_missing method.

HTTP Caching in Ruby on Rails Applications

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’t have to be fetched again from a hard drive or SSD.