📖 Scenario: You are working on a Rails app that manages a list of books. You need to update the database to add a new column for the book's genre and later remove the published_year column.
🎯 Goal: Create a Rails migration to add a genre column of type string to the books table, then create another migration to remove the published_year column from the same table.
📋 What You'll Learn
Create a migration file to add a genre column of type string to the books table
Create a migration file to remove the published_year column from the books table
Use the correct Rails migration methods add_column and remove_column
Name the migration classes correctly following Rails conventions
💡 Why This Matters
🌍 Real World
Adding and removing columns is a common task when evolving a Rails app's database to support new features or remove outdated data.
💼 Career
Rails developers frequently write migrations to update database schemas safely and maintain data integrity during app development.
Progress0 / 4 steps
1
Create migration to add genre column
Create a migration class called AddGenreToBooks that inherits from ActiveRecord::Migration[7.0]. Inside the change method, add a genre column of type string to the books table using add_column.
Ruby on Rails
Hint
Use add_column :books, :genre, :string inside the change method.
2
Create migration to remove published_year column
Create a migration class called RemovePublishedYearFromBooks that inherits from ActiveRecord::Migration[7.0]. Inside the change method, remove the published_year column from the books table using remove_column.
Ruby on Rails
Hint
Use remove_column :books, :published_year inside the change method.
3
Add migration timestamps
Add the timestamps method call inside the change method of the AddGenreToBooks migration class to track when records are created or updated.
Ruby on Rails
Hint
Call timestamps inside the change method to add created_at and updated_at columns.
4
Add reversible migration for removing column
Modify the change method in the RemovePublishedYearFromBooks migration class to use reversible with remove_column and add_column so the migration can be rolled back safely. Remove the timestamps call from the AddGenreToBooks migration.
Ruby on Rails
Hint
Use reversible do |dir| with dir.up and dir.down blocks to safely remove and add back the column.
Remove the timestamps call from the first migration.
Practice
(1/5)
1. What is the primary purpose of adding a column in a Rails migration?
easy
A. To rename an existing table
B. To delete old data from the database
C. To create a new database table
D. To store new types of data in an existing database table
Solution
Step 1: Understand what adding a column means
Adding a column means extending an existing table to hold more data fields.
Step 2: Identify the purpose of adding columns in Rails migrations
It allows storing new types of data without creating a new table or deleting data.
Final Answer:
To store new types of data in an existing database table -> Option D
Quick Check:
Adding columns = storing new data [OK]
Hint: Adding columns means expanding tables to hold new data [OK]
Common Mistakes:
Confusing adding columns with creating tables
Thinking adding columns deletes data
Mixing up adding columns with renaming tables
2. Which of the following is the correct syntax to add a column named age of type integer to the users table in a Rails migration?
easy
A. add_column :users, :age, :integer
B. add_column users, age, integer
C. add_column(:users, :age, integer)
D. add_column(:users, age, :integer)
Solution
Step 1: Recall the correct method signature for adding a column
The method is add_column(table_name, column_name, type) with symbols for names.
Hint: Use symbols and commas: add_column :table, :column, :type [OK]
Common Mistakes:
Omitting colons before symbols
Using parentheses incorrectly
Missing commas between arguments
3. Given this migration code:
class AddNicknameToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :nickname, :string
end
end
What will happen after running rails db:migrate?
medium
A. An error will occur because the migration is missing a remove_column
B. The users table will be deleted
C. A new column 'nickname' of type string will be added to the users table
D. The nickname column will be removed from the users table
Solution
Step 1: Understand the migration code
The migration adds a string column called nickname to the users table inside the change method.
Step 2: Know what rails db:migrate does
It applies the change method, adding the new column to the database table.
Final Answer:
A new column 'nickname' of type string will be added to the users table -> Option C
Quick Check:
add_column in change adds the column [OK]
Hint: add_column inside change adds the column on migrate [OK]
Common Mistakes:
Thinking remove_column is required to add columns
Confusing add_column with deleting tables
Assuming migration deletes columns by default
4. You wrote this migration to remove the birthdate column from users:
class RemoveBirthdateFromUsers < ActiveRecord::Migration[7.0]
def change
remove_column :users, :birthdate
end
end
When running rails db:migrate, you get an error. What is the likely cause?
medium
A. remove_column requires the column type as a third argument in change method
B. remove_column cannot be used inside change method
C. The migration class name is incorrect
D. You must use add_column to remove a column
Solution
Step 1: Recall remove_column syntax in change method
When using remove_column inside change, you must provide the column type as the third argument.
Step 2: Check the migration code
The code misses the column type, causing an error during migration.
Final Answer:
remove_column requires the column type as a third argument in change method -> Option A
Quick Check:
remove_column in change needs column type [OK]
Hint: Provide column type when removing columns in change method [OK]
Common Mistakes:
Omitting column type argument
Trying to remove columns with add_column
Misnaming migration class
5. You want to add a status column to orders with default value 'pending' and then remove the old_status column safely. Which migration code correctly does this?
hard
A. def change
add_column :orders, :status, :string
remove_column :orders, :old_status
end
B. def change
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status, :string
end
C. def change
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status
end
D. def up
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status, :string
end
def down
remove_column :orders, :status
add_column :orders, :old_status, :string
end
Solution
Step 1: Understand adding with default and removing columns safely
Adding a column with default works in change. Removing a column in change requires the type for reversibility.
Step 2: Analyze options for correctness and safety
def change
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status, :string
end uses def change
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status, :string
end with default on add and type on remove for safe migration and rollback.
Final Answer:
def change
add_column :orders, :status, :string, default: 'pending'
remove_column :orders, :old_status, :string
end -> Option B
Quick Check:
change method with type for remove and default for add [OK]
Hint: Use change with column type for safe remove_column [OK]
Common Mistakes:
Omitting column type when removing
Not providing default value when needed
Using change method without rollback safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.