Adding and removing columns lets you change the structure of your database tables. This helps you store new types of information or clean up old data.
Adding and removing columns in Ruby on Rails
class AddColumnNameToTableName < ActiveRecord::Migration[7.0] def change add_column :table_name, :column_name, :data_type end end class RemoveColumnNameFromTableName < ActiveRecord::Migration[7.0] def change remove_column :table_name, :column_name end end
Replace table_name with your table's name, and column_name with the column you want to add or remove.
Specify the data_type like :string, :integer, or :boolean when adding columns.
When removing columns, you do not need to specify the data_type.
age to the users table.class AddAgeToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :age, :integer end end
nickname column from the users table.class RemoveNicknameFromUsers < ActiveRecord::Migration[7.0] def change remove_column :users, :nickname end end
This example shows two migrations. The first adds an email column to the customers table. The second removes it. You run these migrations with rails db:migrate to update your database.
class AddEmailToCustomers < ActiveRecord::Migration[7.0] def change add_column :customers, :email, :string end end class RemoveEmailFromCustomers < ActiveRecord::Migration[7.0] def change remove_column :customers, :email end end
Always back up your database before removing columns to avoid losing important data.
Use rails db:rollback to undo the last migration if needed.
Adding columns with default values or not-null constraints may require extra steps.
Adding columns lets you store new data in your tables.
Removing columns cleans up unused data and keeps your database tidy.
Migrations are the safe way to change your database structure step-by-step.
Practice
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 DQuick Check:
Adding columns = storing new data [OK]
- Confusing adding columns with creating tables
- Thinking adding columns deletes data
- Mixing up adding columns with renaming tables
age of type integer to the users table in a Rails migration?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.Step 2: Check each option for correct syntax
add_column :users, :age, :integer uses symbols and correct commas: add_column :users, :age, :integer.Final Answer:
add_column :users, :age, :integer -> Option AQuick Check:
Correct syntax uses symbols and commas [OK]
- Omitting colons before symbols
- Using parentheses incorrectly
- Missing commas between arguments
class AddNicknameToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :nickname, :string
end
endWhat will happen after running
rails db:migrate?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 CQuick Check:
add_column in change adds the column [OK]
- Thinking remove_column is required to add columns
- Confusing add_column with deleting tables
- Assuming migration deletes columns by default
birthdate column from users:class RemoveBirthdateFromUsers < ActiveRecord::Migration[7.0]
def change
remove_column :users, :birthdate
end
endWhen running
rails db:migrate, you get an error. What is the likely cause?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 AQuick Check:
remove_column in change needs column type [OK]
- Omitting column type argument
- Trying to remove columns with add_column
- Misnaming migration class
status column to orders with default value 'pending' and then remove the old_status column safely. Which migration code correctly does this?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 usesdef change add_column :orders, :status, :string, default: 'pending' remove_column :orders, :old_status, :string endwith 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 BQuick Check:
change method with type for remove and default for add [OK]
- Omitting column type when removing
- Not providing default value when needed
- Using change method without rollback safety
Ready to prove it?
Pass the test and this pattern moves unknown
