ActiveRecord: only_columns

In Rails, we’ve always had ignored_columns to tell ActiveRecord: “Ignore these columns, even if they exist in the database.” But what if we want the opposite? What if we have a table with dozens of columns and our specific service only needs three?

The rename_schema Migration Helper

Schemas are essential for organizing tables or managing multi-tenant architectures. While Rails has long supported basic database operations, renaming a schema used to require dropping down into raw SQL. Since Rails 7.0, the rename_schema method has brought this capability directly into the ActiveRecord migration DSL for those using PostgreSQL.

Update_columns with touch

The methods update_columns and update_column are well-known for their speed because they bypass all Rails validations and callbacks. However, they had a major “side effect”: by default, they wouldn’t update the updated_at timestamp.

ActiveRecord: Improving Readability with Array#inquiry

In Rails, we are used to using the .inquiry method on strings (like the famous Rails.env.development?). However, ActiveSupport also extends this functionality to Arrays, transforming a simple list into an object capable of answering questions semantically.

Beyond db:seed: How to Use db:seed:replant in Rails

Every Rails developer is familiar with rails db:seed, the command used to populate the database with initial data. But what happens when you modify your seeds.rb file and want to start fresh without deleting the entire database? That’s where rails db:seed:replant comes in.

Understanding extend self

In Ruby, we often create modules to serve as libraries of utility functions. You’ve likely seen modules in various projects that use extend self right at the top. But what does it actually do, and why not just use def self.method?

ActionView: relative_time_in_words

Most Rails applications rely on the popular time_ago_in_words helper to show how much time has passed since an event. However, when dealing with future events or very recent dates, saying “in 20 hours” isn’t always the best way to communicate with a user.

ActiveRecord: Understanding CurrentAttributes

current_user is a daily companion in Controllers and Views. However, the moment you need that information inside a Model to validate a business rule, things get tricky. Many developers end up passing the user as a parameter everywhere. But did you know Rails has a native solution for this?

ActiveRecord: Consistent delete_all and update_all

ActiveRecord excels at providing methods that balance convenience and performance. Among the most powerful are delete_all and update_all, which execute bulk operations directly in SQL. However, until recently, they behaved inconsistently when used with certain query methods like limit or distinct.

Elegant Enum Sorting with in_order_of

ActiveRecord enums are a convenient way to map human-readable states to integers stored in the database. They make your models more expressive and your code easier to read. However, sorting records by enum values can be tricky when the default numeric order doesn’t match your business logic.

The Difference Between resource and resources in Rails

Rails provides two closely related routing helpers — resource and resources — and although they look similar, they serve different purposes. Understanding the distinction between them is essential for designing clean, intention-revealing routes in a Rails application.

Send Emails in Bulk with deliver_all_later

This year, Rails introduced a new method for ActionMailer called deliver_all_later. This feature simplifies enqueuing multiple emails for delivery through Active Job. Instead of sending each email individually, you can now enqueue a batch of emails, allowing them to be sent asynchronously when their respective jobs run.

Match HTTP Methods with current_page?

When building a Rails app, you often need to create navigation links that highlight the current page. However, Rails’ default current_page? helper only works with GET and HEAD requests. This can be problematic when dealing with forms or actions that use other HTTP methods like POST, PUT, or PATCH. This feature was introduced in this PR

Rails 8.1: Open Error Files Directly in Your Favorite Editor

Quick iteration and efficient debugging are crucial for productivity. One feature that has made its way into Rails 8.1.0 makes the debugging experience even smoother by allowing you to open the file that caused an error directly in your favorite editor — straight from the error page. No more manually searching for the file that triggered the issue.

Action View Cache Helpers in Rails

Caching is one of the key techniques to optimize the performance of web applications, and Rails makes it easy to implement caching with various built-in helpers. These helpers help us cache fragments, partials, or even entire views, significantly reducing the load on your server by storing previously rendered content and reusing it when needed.

How has_secure_password Works in Ruby on Rails

Securely handling user passwords is crucial; thankfully, Rails provides a straightforward and powerful method for password management with has_secure_password. This method ensures that passwords are never stored in plain text, making your applications more secure by default.

ActiveJob Continuations: Handling Jobs in Rails

When building applications that require long-running background jobs, interruptions and restarts are inevitable. Whether due to server restarts, manual interventions, or just the natural limitations of background job queues, you need a way to make sure jobs can resume from where they left off.

How *_was Method Works in ActiveRecord

Being able to detect changes in model attributes is often essential for building reactive, state-aware systems in Rails. Whether you’re logging updates, triggering side effects, or enforcing validations, it helps to know what a value used to be before it was changed.

Understanding Rails.error.report in Rails 7+

For Rails developers, understanding how to effectively handle and report errors is a critical skill. With the introduction of the Error Reporter API in Rails 7, a standardized and powerful mechanism for managing exceptions across your application has emerged.

Chaining Transformations with .then in Ruby

Have you ever found yourself writing a series of operations in Ruby that felt a bit too verbose? The then method, introduced in Ruby 2.6, is a clean and elegant way to chain transformations on a value, avoiding intermediate variables and improving code clarity.

Sanitizing HTML in Rails with strip_tags

In some API responses, it’s necessary to return plain text even when the original method outputs HTML. This can be challenging when the source method can’t be modified and the response format must follow strict requirements.

How to Exclude Inherited Fields in Rails Serializers

When working with ActiveModel::Serialization in a Ruby on Rails API, it’s common to organize serializers using inheritance—especially when building and maintaining different versions of your API.

A Quick Guide to Ruby's tally Method

Ruby 2.7 introduced a small but powerful method: tally. If you’ve ever written code to count how many times each item appears in a collection, you’re going to love this.

Simplify Nested Routes in Rails with shallow: true

When dealing with nested resources in Ruby on Rails, long and deeply nested URLs can quickly become a problem. That’s where the shallow option comes in.

Ruby's zip: A way to merge arrays

When you need to combine multiple arrays element by element in Ruby, the zip method is just what you need. It’s concise, expressive, and ideal for transforming structured data—especially when used with to_h or map.