The Active Record pattern: keep row data and persistence behavior together
Active Record represents a database row as an object that carries its data, persistence operations, and related domain behavior. A Rails `ActiveRecord::Base` model is a well-known implementation. Load a row, change an attribute, and call `save`; the framework issues SQL, so the pattern reduces repetitive mapping code without hiding SQL, transactions, or schema constraints. [Martin Fowler: Active Record](https://martinfowler.com/eaaCatalog/activeRecord.html) [Rails 8.1: Active Record Basics](https://guides.rubyonrails.org/active_record_basics.html)
The Active Record pattern: keep row data and persistence behavior together
Active Record represents a database row as an object that carries its data, persistence operations, and related domain behavior. A Rails ActiveRecord::Base model is a well-known implementation. Load a row, change an attribute, and call save; the framework issues SQL, so the pattern reduces repetitive mapping code without hiding SQL, transactions, or schema constraints. Martin Fowler: Active Record Rails 8.1: Active Record Basics
This article explains the design pattern and uses the Rails 8.1 guide as a concrete example (official guide checked 2026-11-23). Code is illustrative and was not run. “Active Record” here means the specific design pattern, not every ORM.
What does an Active Record object do?
Relational databases store data in rows and columns. In this pattern, an application object usually wraps one row and exposes column values as attributes. It can also hold behavior closely related to that data and provide ways to read from and write to the database. Callers use model methods instead of repeatedly writing SQL and converting result rows into objects. Martin Fowler describes Active Record as an object that wraps a table row, encapsulates database access, and adds domain logic to that data. Martin Fowler: Active Record
The conceptual path of one record from request to storage looks like this:
Application → Book.find(42) → Book object (attributes from a books row)
↓ change a value; check a rule
book.save → UPDATE books ... → DatabaseThe arrows show the conceptual call flow. Generated SQL and execution timing depend on the method, configuration, and database.
How does Rails work with a model-backed row?
In Rails 8.1, a model that inherits from ApplicationRecord becomes an Active Record model. By convention, Book maps to the books table, and columns appear as object attributes. The model connects the table and columns to methods such as find, create, and save, which are translated into SQL. Rails 8.1: models and CRUD
class Book < ApplicationRecord
validates :title, presence: true
def display_label
"#{title} — #{author}"
end
end
book = Book.find(42) # load a row as an object
book.title = "The Hobbit"
book.save! # persist the changedisplay_label illustrates behavior that is close to the model’s data. find retrieves the record with the given key, and save! persists changes. This example explains Rails 8.1 model and save behavior; it does not report an executed result. Rails’ ordinary save methods run model validations. If validation fails, save returns false and save! raises an exception; create and update also validate. Rails 8.1: validations
Application-level validations help explain input errors, but they do not replace database constraints. Other applications or SQL tools can write to the same table. For example, Rails’ uniqueness validation queries for an existing value before saving; two concurrent connections can both pass that check. A unique database index is needed to prevent the duplicate. Check model validations and schema constraints together. Rails 8.1: limits of uniqueness validation
How is Active Record related to an ORM?
Object-relational mapping (ORM) is the broader technique of mapping objects to relational tables. Active Record is one design within that category: persistence behavior lives on the data object. Rails describes Active Record both as an ORM framework and as an implementation of the Active Record pattern. An ORM does not have to use this pattern. Rails 8.1: Active Record pattern and ORM
The key comparison is with Data Mapper. With Active Record, domain objects know more directly about persistence and database structure. Data Mapper puts a separate mapping layer between domain objects and the database, which can keep them independent when the domain model is complex or differs substantially from the tables. That separation requires extra mapper and mapping rules. These are design trade-offs inferred from the patterns’ responsibility boundaries, not universal selection rules. Martin Fowler: Data Mapper
| Criterion | Active Record | Data Mapper |
|---|---|---|
| Persistence responsibility | The model exposes reads and writes | A separate mapper moves data between objects and the database |
| Relation to schema | Models tend to match table rows closely | Domain objects can stay separate from table structure |
| Where it can fit well | CRUD-oriented applications, simpler models, quick implementation | Complex domains or a large gap between object and table structures |
| Cost to consider | Database structure and model behavior can become coupled | Mapper code and mapping rules must be designed and maintained |
This table turns the pattern definitions into practical selection questions. The actual complexity depends on team conventions, framework features, tests, and database requirements.
What should you watch for behind the convenience?
Model methods do not eliminate queries. The Rails guide explains that Active Record CRUD methods issue SQL against the database. Inspect generated SQL and affected row counts for repeated reads, large result sets, bulk changes, and transaction boundaries. Repeatedly loading related records can create an N+1 query problem. This is not unique to Active Record; it can arise from ORM relationship loading and query use. Eager loading or set-based SQL may help, depending on the case. Rails 8.1: CRUD and SQL Rails 8.1: N+1 queries and eager loading
Concentrating side effects in callbacks can make save-time work hard to trace. Email or an external API call is not automatically undone when a database transaction rolls back, so decide when it should run. Rails provides after_commit and after_rollback callbacks associated with transaction completion and documents them as useful for interactions with external systems. Rails 8.1: transaction callbacks
Do not assume that model validation alone guarantees data integrity or that a method name means a value is already durable. Check validations alongside database constraints, inspect SQL and transaction boundaries, and ask whether it makes sense to keep the schema and domain behavior together in the model. That is how to weigh Active Record’s convenience against its coupling cost.