Rails' date and time helpers are great. They save us from duplicating simple add-duration-to-time logic across our applications and make the code more readable. However, complex date manipulations are dangerous places full of edge-cases. This article discusses some of them. 😅
PostgreSQL and MySQL are great for structuring data using relationships, but what if you don't always know the structure up-front? That's where
ActiveRecord::Store really shines. It's like NoSQL, without changing databases.
You've probably used
Rails.cache to read, write, and fetch cached data in Rails—but did you know you can also work with counters? In this series, Jonathan Miles introduces us to some of the lesser-known tools hidden in your Rails codebase.
The #descendants method is part of Rails. It returns all subclasses that inherit from a given class. In this article, Jonathan Miles shows us how to use this method and how it's implemented. It's a great lesson in the ins and outs of Ruby's object model.
Race conditions are arguably the most insidious kind of bug; they're intermittent, subtle, and most likely to occur in production. ActiveRecord's
update_counter provides us with a convenient way to avoid race conditions when incrementing or decrementing values in the database. In this article, Jonathan Miles shows us how to use it, how it's implemented, and other approaches to avoiding race conditions.
If you've ever checked the environment in your Rails app with Rails.env.production? you've used a fascinating little utility class called StringInquirer. In this post, Jonathan Miles dives into the rails codebase to show us exactly how StringInquirer works and how we can bring a little of its magic to our own apps.
The fastest web page is one you've already loaded. Browsers love to avoid round-trips by caching assets. And HTTP provides ways for us to tell browsers what's changed and what hasn't - so they make the right decisions. In this article, Jonathan Miles introduces us to HTTP caching and shows us how to implement it in Rails.
ActiveRecord makes accessing your database easy, but it can also help make it faster by its intelligent use of caching. In this article, Jonathan Miles shows us the tricks that Rails uses to ensure that your database isn't doing more work than it needs to.
If you've ever built a UI in Rails, you've probably noticed that views tend to get slower over time. That's because adding features to a UI often means adding DB queries to the view. They add up. Fortunately, Rails provides us with an easy-to-apply band-aid in the form of view caching. In this article, Jonathan Miles introduces us to view caching, discusses when it's appropriate to use, and covers common pitfalls to watch out for.
Sometimes when your app is slow, it's not your fault. Your code might be optimized to the teeth, but it won't matter if it has to perform intrinsically slow tasks, like fetching data from an external API. In these situations, Rails' low-level caching can be a life-saver. But caching is infamously tricky. It's dangerous to go alone. In this article, Jonathan Miles guides us through the landscape of low-level caching. He covers the basics, but more importantly, digs into essential details of cache invalidation and points out common pitfalls.
Whoever first said that "the fastest code is no code" must have really liked memoization. After all, memoization speeds up your application by running less code. In this article, Jonathan Miles introduces us to memoization. We'll learn when to use it, how to implement it in Ruby, and how to avoid common pitfalls. Buckle up!
We've all been there. You're clicking around your Rails application, and it just isn't as snappy as it used to be. You start searching for a quick-fix and find a lot of talk about caching. Take your existing app, add some caching, and voila, a performance boost with minimal code changes. However, it's not this simple. Like most quick fixes, caching can have long-term costs. In this article, Jonathan Miles discusses what caching is and what can go wrong, as well as explains non-caching strategies you can use to speed up your Rails app.
We've all worked with tightly-coupled code. If a butterfly flaps its wings in China, the unit tests break. Maintaining a system like this is...unpleasant. In this article, Jonathan Miles dives into the origins of tight-coupling. He demonstrates how you can use dependency injection (DI) to decouple code. Then he introduces a novel decoupling technique based on delegation that can be useful when DI is not an option.