Chapter 6: Models, Migrations, and the Database

Chapter 6: Models, Migrations, and the Database

AI Reading

Quick summary of this article

This chapter introduces how Rails manages databases through Active Record, an ORM that maps database tables to Ruby objects. You'll learn to create models, run migrations to alter database structure, and perform CRUD operations using Ruby commands.

  • Use rails generate model Post title:string body:text to create a model and migration, then apply it with rails db:migrate.
  • Migrations define database changes, like creating a table with columns for title (string) and body (text).
  • Active Record provides simple methods for CRUD: Post.new + save to create, Post.find(1) to read, update to modify, and destroy to delete.
  • The Rails console (rails console) lets you test database operations directly from the terminal.
  • Practice by generating the Post model, creating records with Post.create, and verifying them with Post.count and Post.all.

Chapter 6: Models, Migrations, and the Database

Rails uses Active Record, an ORM (Object-Relational Mapping) that lets you work with database tables using plain Ruby objects. Every table has a model, and every change to the table structure is recorded in a migration.

In this chapter you will create a model, run migrations, and perform basic database operations (create, read, update, delete) from Ruby.

💾 Generating a Model

To create a model called Post with a title and a body, run:

rails generate model Post title:string body:text
rails db:migrate

The first command creates the model file app/models/post.rb and a migration. The db:migrate command applies the migration and creates the posts table in your database.

📜 Understanding Migrations

A migration describes a change to the database. Here is the migration created by the command above:

# db/migrate/xxx_create_posts.rb
class CreatePosts < ActiveRecord::Migration[7.0]
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end
end

The t.string :title line creates a text column named title, and t.text :body creates a longer text column for the body.

🚀 CRUD with Active Record

Once the model exists, you can work with the database using Ruby:

# Create
post = Post.new(title: "Hello Rails", body: "My first post")
post.save

# Read
post = Post.find(1)
all_posts = Post.all

# Update
post.update(title: "Updated title")

# Delete
post.destroy

🔍 Using the Rails Console

To try these commands without a browser, open the Rails console:

rails console
Post.create(title: "Console post", body: "Created from the terminal")
Post.all.each { |p| puts p.title }

🏠 Practice Exercises

# 1. Create the Post model
rails generate model Post title:string body:text
rails db:migrate
# 2. In rails console, create and read records
rails console
p1 = Post.create(title: "First", body: "Hello")
p2 = Post.create(title: "Second", body: "World")
Post.count
Post.all.map(&:title)

In the next chapter you will build a complete welcome page using everything you have learned so far.

Leave a Reply

Your email address will not be published. Required fields are marked *