The database folder in Rails keeps all files related to your app's data. Migrations help you change the database structure safely over time.
When you want to create a new table to store data like users or products.
When you need to add or remove columns from an existing table.
When you want to rename or change the type of a column in your database.
When you want to keep track of changes to your database structure in a clear, organized way.
When you need to share database changes with your team or deploy them to a server.
Syntax
Ruby on Rails
rails generate migration MigrationName
# Inside the generated migration file:class MigrationName < ActiveRecord::Migration[7.0]
def change
create_table :table_name do |t|
t.string :column_name
t.timestamps
end
end
end
Migration files live in the db/migrate folder.
The change method describes what changes to make to the database.
Examples
This creates a new users table with name, email, and timestamp columns.
Ruby on Rails
rails generate migration CreateUsers
# Migration file example:class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
This adds an age column of type integer to the existing users table.
Ruby on Rails
rails generate migration AddAgeToUsers age:integer
# Migration file example:class AddAgeToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :age, :integer
end
end
This removes the email column from the users table.
Ruby on Rails
rails generate migration RemoveEmailFromUsers
# Migration file example:class RemoveEmailFromUsers < ActiveRecord::Migration[7.0]
def change
remove_column :users, :email, :string
end
end
Sample Program
This example shows how to create a migration to add a books table with title and author columns. Running rails db:migrate applies the change to the database.
Ruby on Rails
# Terminal command to create migration
rails generate migration CreateBooks title:string author:string
# db/migrate/20240601000000_create_books.rbclass CreateBooks < ActiveRecord::Migration[7.0]
def change
create_table :books do |t|
t.string :title
t.string :author
t.timestamps
end
end
end
# After creating migration, run:
rails db:migrate
# This creates the books table with title, author, and timestamps.
OutputSuccess
Important Notes
Always write clear migration names to understand what changes they make.
Run rails db:migrate to apply migrations to your database.
You can rollback migrations with rails db:rollback if needed.
Summary
The db folder holds your database setup and migrations.
Migrations let you change your database structure step-by-step.
Use migrations to create, update, or delete tables and columns safely.
Practice
(1/5)
1. What is the main purpose of the db/migrate folder in a Rails project?
easy
A. To store migration files that change the database structure step-by-step
B. To hold all the Ruby model files for the application
C. To keep the application configuration files
D. To store static assets like images and stylesheets
Solution
Step 1: Understand the role of the db/migrate folder
This folder contains migration files that describe changes to the database schema over time.
Step 2: Differentiate from other folders
Model files are in app/models, configs in config, and assets in app/assets.
Final Answer:
To store migration files that change the database structure step-by-step -> Option A
Quick Check:
Migration files = db/migrate folder [OK]
Hint: Migrations live in db/migrate to track database changes [OK]
Common Mistakes:
Confusing migrations with model files
Thinking assets or configs are in db/migrate
Assuming db/migrate holds seed data
2. Which command correctly generates a new migration file in Rails?
easy
A. rails new migration AddAgeToUsers age:integer
B. rails generate migration AddAgeToUsers age:integer
C. rails create migration AddAgeToUsers age:integer
D. rails make migration AddAgeToUsers age:integer
Solution
Step 1: Recall the Rails command for generating migrations
The correct command is rails generate migration followed by the migration name and fields.
Step 2: Check the options for correct syntax
Only rails generate migration AddAgeToUsers age:integer uses the exact Rails command syntax for migration generation.
Final Answer:
rails generate migration AddAgeToUsers age:integer -> Option B
Quick Check:
Use rails generate migration to create migrations [OK]
Hint: Use 'rails generate migration' to create migration files [OK]
Common Mistakes:
Using 'rails create' instead of 'rails generate'
Confusing 'rails new' with migration commands
Using 'rails make' which is invalid
3. Given this migration file content:
class CreateBooks < ActiveRecord::Migration[7.0]
def change
create_table :books do |t|
t.string :title
t.integer :pages
t.timestamps
end
end
end
What will happen when you run rails db:migrate?
medium
A. Nothing happens because migrations only update existing tables
B. An error occurs because the migration syntax is wrong
C. The existing 'books' table will be deleted
D. A new table named 'books' with columns 'title', 'pages', and timestamps will be created
Solution
Step 1: Analyze the migration code
The migration defines creating a new table called 'books' with columns 'title' (string), 'pages' (integer), and timestamps.
Step 2: Understand what rails db:migrate does
It runs pending migrations, applying the changes to the database schema, here creating the 'books' table.
Final Answer:
A new table named 'books' with columns 'title', 'pages', and timestamps will be created -> Option D
Quick Check:
Create table migration runs successfully [OK]
Hint: Create_table migrations add new tables when migrated [OK]
Common Mistakes:
Thinking migration deletes tables
Assuming syntax error without checking code
Believing migrations only update, not create
4. You wrote this migration:
class AddEmailToUsers < ActiveRecord::Migration
def change
add_column :users, :email, :string
end
end
But running rails db:migrate gives an error: undefined method 'add_column'. What is the likely cause?
medium
A. The migration class should inherit from ActiveRecord::Migration[7.0] but it does
B. The migration file is missing the def change method
C. The migration class is missing the version number in inheritance
D. The migration file name does not match the class name
Solution
Step 1: Check the migration inheritance
Rails 7 requires specifying the migration version in the class inheritance, e.g., ActiveRecord::Migration[7.0].
Step 2: Identify the error cause
If the version is missing or incorrect, Rails may not recognize migration methods like add_column, causing the error.
Final Answer:
The migration class is missing the version number in inheritance -> Option C
Quick Check:
Migration version number required for method recognition [OK]
Hint: Always specify migration version in class inheritance [OK]
Common Mistakes:
Ignoring migration version in class inheritance
Assuming method missing means syntax error
Not matching file name and class name
5. You want to rename a column username to login in the users table using a migration. Which migration code correctly does this?
hard
A. def change
rename_column :users, :username, :login
end
B. def change
change_column :users, :username, :login
end
C. def change
remove_column :users, :username
add_column :users, :login, :string
end
D. def change
rename_table :users, :login
end
Solution
Step 1: Understand the goal
You want to rename a column, not change its type or remove and add a new one.
Step 2: Identify the correct migration method
rename_column changes a column's name safely without losing data.
Step 3: Check options
def change
rename_column :users, :username, :login
end uses rename_column :users, :username, :login, which is correct. def change
change_column :users, :username, :login
end wrongly uses change_column which changes type, not name. def change
remove_column :users, :username
add_column :users, :login, :string
end removes and adds columns, which is risky. def change
rename_table :users, :login
end renames the whole table, not a column.
Final Answer:
def change
rename_column :users, :username, :login
end -> Option A
Quick Check:
Use rename_column to rename columns safely [OK]
Hint: Use rename_column to rename columns without data loss [OK]
Common Mistakes:
Using change_column to rename instead of change type
Removing and adding columns loses data
Confusing rename_table with rename_column
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.