ActiveRecord
By the Ruby on Rails project
Active Record is the Object-Relational Mapping (ORM) layer built into Ruby on Rails that implements the Active Record design pattern, where each model class both wraps a database row and contains the persistence logic to save, update, or…
Definition
Active Record is the Object-Relational Mapping (ORM) layer built into Ruby on Rails that implements the Active Record design pattern, where each model class both wraps a database row and contains the persistence logic to save, update, or delete it.
Overview
Active Record is named after — and is a direct implementation of — the Active Record architectural pattern, where a single object combines both data and the database access logic for that data. In practice, a Rails model class like `class User < ApplicationRecord` automatically gains methods such as `.find`, `.save`, `.update`, and `.destroy` simply by inheriting from `ApplicationRecord`, with columns inferred directly from the underlying database table rather than declared explicitly in the class body. Schema changes in a Rails application are managed through Active Record Migrations — Ruby DSL files with `change`, `up`, and `down` methods, run via `rails db:migrate`, that are conceptually similar to what Flyway or Liquibase do in other ecosystems but expressed as idiomatic Ruby rather than SQL or XML. Rails maintains a `schema.rb` (or `structure.sql`) file that represents the cumulative result of all migrations, which new environments can load directly rather than replaying the full migration history. Active Record includes rich support for associations (`has_many`, `belongs_to`, `has_and_belongs_to_many`), validations, callbacks (lifecycle hooks like `before_save`), and scopes for composable query building, all designed around Rails' broader "convention over configuration" philosophy — a table named `users` is automatically mapped to a `User` model with no additional configuration required. Because the Active Record pattern couples the model directly to persistence logic, it's often contrasted with the Data Mapper pattern used by tools like SQLAlchemy's ORM layer or Hibernate's default mode, where model classes stay ignorant of how they're persisted. This trade-off favors developer productivity and rapid prototyping over strict separation of concerns, which is part of why Ruby on Rails became known for fast initial application development.
Key Features
- Models double as both data objects and persistence-logic containers
- Columns automatically inferred from the underlying database table
- Rich association helpers: has_many, belongs_to, has_and_belongs_to_many
- Ruby DSL migrations run via rails db:migrate, with schema.rb tracking cumulative state
- Validations and lifecycle callbacks (before_save, after_create, etc.)
- Composable query scopes and method chaining for building queries
- Convention-over-configuration table/model naming with minimal boilerplate