Active Record Basics
ORM fundamentals.
Active Record Basics is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Active Record?
Active Record is the ORM (Object-Relational Mapper) layer of Rails. It connects Ruby objects to rows in a database table so you work with objects instead of raw SQL.
- A class maps to a table.
- An instance maps to a row.
- An attribute maps to a column.
Defining a Model
A model is a class that inherits from ApplicationRecord (which inherits from ActiveRecord::Base). By convention a User model maps to the users table automatically.
class User < ApplicationRecord
end
# Maps to the 'users' table
# Columns become attributes automaticallyConvention Over Configuration
Active Record uses naming conventions so you write less setup:
- Class names are singular and CamelCase:
User. - Table names are plural and snake_case:
users. - Primary key is
idby default.
Follow conventions and most wiring is automatic.
Creating Records
Create rows with create, or build then save. create instantiates and saves in one call.
user = User.create(name: "Ada", email: "ada@example.com")
# Or in two steps:
other = User.new(name: "Linus")
other.email = "linus@example.com"
other.saveReading Records
Fetch records with finder methods:
User.find(1)by primary key (raises if missing).User.find_by(email: 'a@b.com')by any column (returns nil if missing).User.allfor every row.User.firstandUser.last.
user = User.find(1)
admin = User.find_by(role: "admin")
everyone = User.allUpdating Records
Change attributes then save, or use update to assign and save together. update returns true or false based on validation.
user = User.find(1)
user.update(name: "Ada Lovelace")
# Equivalent two-step form:
user.name = "Ada Lovelace"
user.saveDeleting Records
Remove rows with destroy (runs callbacks and associations) or delete (straight SQL, skips callbacks). Prefer destroy unless you have a reason to bypass callbacks.
user = User.find(1)
user.destroy
# Bulk destroy:
User.where(active: false).destroy_allAttributes and Dirty Tracking
Active Record tracks unsaved changes. Methods like changed?, changes, and name_was tell you what is dirty before you save. This is useful in callbacks and logging.
user = User.find(1)
user.name = "New Name"
puts user.changed? # true
puts user.name_was # old value
puts user.changes.inspectThe schema.rb File
Active Record reads your table structure from the database, not from your model. The current structure is mirrored in db/schema.rb, which is generated from migrations. You rarely edit it by hand.
Where Does SQL Go?
Active Record turns your method calls into SQL behind the scenes. User.find(1) becomes SELECT * FROM users WHERE id = 1 LIMIT 1. You can see the generated SQL with User.where(active: true).to_sql.
sql = User.where(active: true).to_sql
# => SELECT "users".* FROM "users" WHERE "users"."active" = TRUEWhy an ORM Helps
Active Record gives you:
- Database-agnostic code (PostgreSQL, MySQL, SQLite).
- Protection against SQL injection through parameterized queries.
- Readable, object-oriented data access.
You drop to raw SQL only for special cases.
Quick Check
Test your understanding of Active Record basics.
Recap: Active Record Basics
You learned ORM fundamentals:
- A class maps to a table, an instance to a row.
- Convention over configuration handles naming.
- CRUD:
create,find/find_by,update,destroy. - Dirty tracking shows unsaved changes.
- Active Record generates safe SQL for you.
Next we manage the database schema with migrations.
Frequently asked questions
Is the “Active Record Basics” lesson free?
Yes — the full text of “Active Record Basics” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Active Record Basics”?
ORM fundamentals. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Active Record Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Active Record Basics
- Migrations
- Associations
- Validations and Queries