0
0
Ruby on Railsframework~15 mins

Running and rolling back migrations in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Running and Rolling Back Migrations in Rails
📖 Scenario: You are building a simple Rails app to manage a list of books. You need to create a database table for books and then learn how to run and rollback migrations to manage your database schema.
🎯 Goal: Create a migration to add a books table with title and author columns, run the migration to update the database, then rollback the migration to undo the changes.
📋 What You'll Learn
Create a migration file named create_books with title and author columns as strings
Run the migration using the correct Rails command
Rollback the last migration using the correct Rails command
💡 Why This Matters
🌍 Real World
Managing database schema changes safely is essential in real-world Rails applications to keep data consistent and allow easy updates.
💼 Career
Rails developers frequently create, run, and rollback migrations to evolve the database schema during development and deployment.
Progress0 / 4 steps
1
Create a migration file for books table
Generate a migration file named create_books that creates a books table with string columns title and author. Use the Rails generator command exactly as shown.
Ruby on Rails
Need a hint?

Use rails generate migration followed by the migration name and columns with types.

2
Run the migration to update the database
Run the migration to apply the changes to the database using the Rails command rails db:migrate.
Ruby on Rails
Need a hint?

Use rails db:migrate to apply migrations.

3
Rollback the last migration
Rollback the last migration using the Rails command rails db:rollback to undo the changes made by the migration.
Ruby on Rails
Need a hint?

Use rails db:rollback to undo the last migration.

4
Verify migration rollback
Add a command to check the current status of migrations using rails db:migrate:status to verify that the migration was rolled back.
Ruby on Rails
Need a hint?

Use rails db:migrate:status to see which migrations are applied.