ActiveRecord Models & Migrations

ActiveRecord is the part of Ruby on Rails that talks to your database. It's an ORM: it maps Ruby objects to database rows so you can store and query data with plain Ruby instead of SQL.

Learn ActiveRecord Models & Migrations in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll generate a model, write and run migrations, understand the schema, and perform CRUD with create, find, where, update, and destroy.

What You'll Learn in This Lesson

1️⃣ Models and Migrations

A model is a Ruby class inheriting from ApplicationRecord ; it maps to a table automatically. The generator also creates a migration , which you apply with rails db:migrate .

The migration describes the table's columns. Running it builds the table and updates db/schema.rb .

2️⃣ CRUD with ActiveRecord

With a model in place, you store and fetch data using Ruby methods. create inserts, find / where / all read, update changes, and destroy deletes.

Your turn. Fill in the right ActiveRecord method for each task.

For each goal, name the ActiveRecord method you'd call.

📋 Quick Reference — ActiveRecord

Practice quiz

What is ActiveRecord?

  • A CSS framework
  • Rails' ORM that maps Ruby objects to database rows
  • A routing macro
  • A testing tool

Answer: Rails' ORM that maps Ruby objects to database rows. ActiveRecord is Rails' ORM, mapping classes to tables and objects to rows.

A model named Post conventionally maps to which table?

  • post
  • Posts
  • posts
  • tbl_post

Answer: posts. By convention a Post model maps to the plural, lowercase posts table.

Which command generates a model with a migration?

  • rails g model Post title:string
  • rails new model Post
  • rails server Post
  • rails routes Post

Answer: rails g model Post title:string. rails g model Post title:string creates the model class and a matching migration.

What is a migration?

  • A controller action
  • A versioned set of instructions to change the database schema
  • A view template
  • A route helper

Answer: A versioned set of instructions to change the database schema. A migration is a versioned Ruby script describing changes to the database structure.

Which command applies pending migrations to the database?

  • rails db:create
  • rails db:rollback
  • rails server
  • rails db:migrate

Answer: rails db:migrate. rails db:migrate runs pending migrations, updating the schema.

How do you create and save a record in one call?

  • Post.all
  • Post.create(title: "Hi")
  • Post.where(title: "Hi")
  • Post.destroy

Answer: Post.create(title: "Hi"). Post.create(...) builds and saves a new record in a single call.

Which method finds a record by its primary key id?

  • Post.where(5)
  • Post.all(5)
  • Post.find(5)
  • Post.new(5)

Answer: Post.find(5). Post.find(5) returns the record whose id is 5 (raising if none exists).

What does Post.where(published: true) return?

  • A single record
  • A relation of all matching records
  • The count only
  • Nothing

Answer: A relation of all matching records. where returns an ActiveRecord::Relation of every record matching the condition.

Which file reflects the database's current structure?

  • db/schema.rb
  • config/routes.rb
  • app/models/post.rb
  • Gemfile

Answer: db/schema.rb. db/schema.rb is the auto-generated snapshot of the current database structure.

How do you update an existing record's attribute?

  • post.create(title: "New")
  • post.where(title: "New")
  • post.find(title: "New")
  • post.update(title: "New")

Answer: post.update(title: "New"). post.update(title: "New") changes the attribute and saves it.