0
0
Ruby on Railsframework~30 mins

Database folder and migrations in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Database Folder and Migrations in Rails
📖 Scenario: You are building a simple Rails application to manage a list of books. Each book has a title and an author.
🎯 Goal: Create the initial database setup by defining the database folder structure and writing a migration to create a books table with title and author columns.
📋 What You'll Learn
Create the db folder structure with migrate subfolder
Create a migration file to create a books table
Add title and author columns as strings in the migration
Write the migration class with change method
Ensure the migration file name follows Rails timestamp naming convention
💡 Why This Matters
🌍 Real World
Rails applications use migrations to manage database schema changes safely and consistently across development, testing, and production environments.
💼 Career
Understanding migrations is essential for Rails developers to create and modify database tables, enabling them to build data-driven web applications.
Progress0 / 4 steps
1
Create the database folder structure
Create a folder named db and inside it create a folder named migrate to hold migration files.
Ruby on Rails
Need a hint?

Rails stores migration files inside db/migrate folder.

2
Create a migration file for books table
Create a migration file named 20240101000000_create_books.rb inside db/migrate folder. The filename must start with a timestamp like 20240101000000 followed by _create_books.rb.
Ruby on Rails
Need a hint?

Migration files are Ruby classes inheriting from ActiveRecord::Migration.

3
Add columns to the books table in migration
Inside the change method of CreateBooks migration, write code to create a books table with string columns title and author using create_table :books do |t| block.
Ruby on Rails
Need a hint?

Use create_table :books do |t| and inside the block add t.string :title and t.string :author.

4
Complete the migration file
Ensure the migration file 20240101000000_create_books.rb contains the full class CreateBooks inheriting from ActiveRecord::Migration[7.0] with the change method that creates the books table with title and author columns.
Ruby on Rails
Need a hint?

The migration class should be complete with the change method creating the table and columns.