Tag

Rails Internals

28 posts tagged with "Rails Internals"

Understanding ActiveStorage Variants for Image Transformations in Rails

January 16, 2026

Understanding ActiveStorage Variants for Image Transformations in Rails

Image transformations let you show images in the size or format you need. You can create a new version by calling variant on an attachment and passing the desired transformations. When a browser requests that URL, Rails will process the original image on demand and redirect to the generated file.
You Can Now Access Bearer Tokens Directly from Request

December 30, 2025

You Can Now Access Bearer Tokens Directly from Request

The new bearer_token method on the Request object is a nice addition to Rails. It removes boilerplate code and simplifies bearer token extraction. All Rails apps can now access bearer tokens the same way without having to write custom token extraction logic every time and worrying about edge cases.
Has One Attached Active Storage

October 18, 2025

Active Storage Internals: How has_one_attached DSL Works

In this post, we'll explore the internals of has_one_attached method in Active Storage. It covers two interesting patterns, i.e. proxy and change objects. We'll trace the control flow from the model DSL to persistence and uploads, and explain how files are created, detached, and purged.
Active Storage Architecture

October 16, 2025

Active Storage Domain Model: Blobs and Attachments

Active Storage uses two main models: blobs and attachments. Blobs store the uploaded file's metadata and a link to the actual file, while attachments link those files to records. Understanding how they work together makes it easier to manage uploads, generate URLs, and process uploaded files.
How to Inspect the Sequence of Controller Callbacks in Rails

June 17, 2025

How to Inspect the Sequence of Controller Callbacks in Rails

This post shows how to inspect the sequence of before, after, and around callbacks in Rails controllers by adding a small initializer. Useful for understanding callback order in applications with complex controller hierarchies or shared concerns. I learned this trick while reading the Rails tests.
Redirects in Rails: Manual, Helper, and Internals

June 07, 2025

Redirects in Rails: Manual, Helper, and Internals

In this post, we’ll explore how redirects work in Rails: what they are, how to redirect manually, and how the redirect_to method simplifies things. We’ll cover common use cases, security considerations, and even dig into the Rails source to see how redirect_to works under the hood.
Working with the Rails Instrumentation API

February 08, 2025

Working with the Rails Instrumentation API

The Instrumentation API in ActiveSupport serves a dual purpose. You can use it to implement the observer (pub-sub) pattern, as well as benchmark how long it took to execute some action. In this post, we'll learn almost everything you need to know about the Rails Instrumentation API.
How a Ruby Method Becomes a Rails Action: Part One

January 04, 2025

How a Ruby Method Becomes a Rails Action: Part One

In this post, we will explore how a simple Ruby method, when added to a controller, becomes an action in Rails, ready to process incoming HTTP requests and send responses. We'll also trace the path of an incoming HTTP request to a Rails controller action.
A Brief Introduction to Rails Initializers: Why, What, and How

October 19, 2024

A Brief Introduction to Rails Initializers: Why, What, and How

At first glance, Rails initializers seem complex, but they're solving a simple, but important problem: run some code after framework and gems are loaded, to initialize the application. This post covers the basics of initializers, including what they are, how they work, and how Rails implements them.
How to Access Rails Models in a Rake Task

May 19, 2024

How to Access Rails Models in a Rake Task

This article shows how you can access your application models and other constants inside rake tasks by adding the `environment` task as a dependency. We'll also go one level deeper and learn exactly how this task loads the environment by inspecting the Rails source code.
How I Read Rails Source Code

April 12, 2024

How I Read Rails Source Code

Here're two techniques I've found really helpful for reading the Rails codebase, without getting overwhelmed. If you want to dive into the Rails source code for a deeper understanding, but feel intimidated by the sheer size of the codebase and don't know where to begin, this post is for you.
Custom URL Helpers in Rails with the direct Method

February 11, 2024

Custom URL Helpers in Rails with the direct Method

This is the first post in the Campfire deep dive series where we explore the first ONCE product from 37signals to learn and extract useful patterns, ideas, and best practices. This post explores the direct method in the Rails Router that lets you define custom URL helpers for your application.
Prevent Logging Sensitive Data with Rails Parameter Filters

February 01, 2024

Prevent Logging Sensitive Data with Rails Parameter Filters

This article explains why you shouldn't log confidential or user-identifiable information and how to filter it using parameter filtering in Rails. We'll also do a deep dive into the Rails source code to learn exactly how Rails implements parameter filters.
Understanding How Rails Environments Work

January 24, 2024

Understanding How Rails Environments Work

This post covers the basics of environments in Rails, including what they are, how they work and why they're so important. We'll also take a look behind the scenes and dive into the Rails source code to learn exactly how they're implemented, exploring a few interesting "inquiry" classes in Rails.
Rails Internals: A Deep Dive Into Active Job Codebase

December 15, 2023

Rails Internals: A Deep Dive Into Active Job Codebase

Do you want to understand how Active Job works behind the scenes? Reading and understanding the source code is one of the best ways to learn a framework (or anything). This post breaks it all down for you by tracing the journey of a Rails background job, from its creation to execution.
How respond-to Method Works in Rails

December 07, 2023

How respond-to Method Works in Rails

The respond_to method allows the controller to select the appropriate response format based on the request's Accept header or the request URL. You can also use it to handle variants for different screens. This post covers the basics of this method: what it is, how it works and why it's important.
What Happens When You Run Rails dev:cache Command?

September 30, 2023

What Happens When You Run Rails dev:cache Command?

You can enable caching in development mode by running the `rails dev:cache` command in terminal. Ever wondered what that command does, and how exactly does it tell Rails to start caching stuff in your application? Let's take a peek behind the curtain to see exactly what's going on.
How to Access a Ruby Hash with both String and Symbol Keys

September 27, 2023

How to Access a Ruby Hash with both String and Symbol Keys

Sometimes, you receive a Hash key as a method parameter or via user input, and you want to make the Hash understand that key as-is, without worrying if the key is a string or a symbol. ActiveSupport's `HashWithIndifferentAccess` class lets you accomplish this in a convenient way.
How to Debug and Step-Through Rails Codebase

September 16, 2023

How to Debug and Step-Through Rails Codebase

Do you want to read the Rails source code for a deeper understanding of the framework, but feel intimidated by the sheer size of the codebase, or don't know where to start? Start with a specific feature, insert a breakpoint, and step through the method line-by-line. This article shows how.
How Module Autoloading Works in Ruby and Rails

June 14, 2023

How Module Autoloading Works in Ruby and Rails

Autoloading allows you to speed up the initialization of your library by lazily loading the code only when you need it. This post explains how the autoload method works in Ruby and how Rails overrides this method to provide its own implementation that follows the Rails naming conventions.
Generating Secure Tokens on Your ActiveRecord Models

May 31, 2023

Generating Secure Tokens on Your ActiveRecord Models

You must have used the `has_secure_password` macro in Rails. Did you know Rails also provides a `has_secure_token` macro to generate unique tokens on your models? In this article, we'll learn how it works and we'll also see how Rails implements it behind the scenes.
Concerns in Rails: Everything You Need to Know

May 20, 2023

Concerns in Rails: Everything You Need to Know

Concerns are an important concept in Rails that can be confusing to understand for those new to Rails as well as seasoned practitioners. This post explains what concerns are, how they work, and how & when you should use them to simplify your code, with practical, real-world examples.
How to Access Hash Values with Methods Using OrderedOptions

April 15, 2023

How to Access Hash Values with Methods Using OrderedOptions

Have you ever wanted to create a hash where you could access the values like methods on an object? The OrderedOptions class in Rails lets you do just that. This post shows you how. We'll also explore how Rails implements this feature using Ruby's metaprogramming features.

April 04, 2023

Readonly Attributes in Rails

In this article, we'll learn how to mark certain attributes as readonly on your active record models, to prevent them from any future updates after the record is created and saved to the database. We'll also learn how Rails implements this feature behind the scenes.
Understanding the Attribute Assignment API in Rails

March 23, 2023

Understanding the Attribute Assignment API in Rails

In this post, we will explore the `AttributeAssignment` module in Rails, which allows you to set an object's attributes by passing in a hash, a feature commonly used by Active Record models. We'll also learn a little metaprogramming along the way.
How Rails Authenticity Tokens Protect Against CSRF Vulnerability

February 17, 2023

How Rails Authenticity Tokens Protect Against CSRF Vulnerability

Rails protects your web application from CSRF attack by including an authenticity token in the HTML forms. This token is also stored in the user's session. Upon receiving a request, Rails compares these two tokens to decide if the request is verified.

December 29, 2022

What Happens When You Call render? Let's Understand the Rails Rendering Process

This article explains the Rails rendering process in the context of returning JSON data from the controller. Hopefully, it will make it clear what really happens when you call the render method from the controller.

August 12, 2022

Active Job: How does it work?

In this article, I explain the journey of a background job, from its creation to execution. We will learn how it’s configured and enqueued, and how an adapter invokes it. We will also see the different ways to configure a job.