Hint: Use 'AddColumnToTable' naming with column:type [OK]
Common Mistakes:
Using 'CreatePosts' instead of 'AddTitleToPosts'
Wrong order or missing 'To' in migration name
Incorrect column type syntax
3. Given this migration code:
class AddAgeToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :age, :integer
end
end
What happens after running rails db:migrate?
medium
A. Raises a syntax error due to missing migration version
B. Removes the 'age' column from the 'users' table
C. Creates a new 'users' table with an 'age' column
D. Adds an integer column named 'age' to the 'users' table
Solution
Step 1: Analyze the migration class
The migration inherits from ActiveRecord::Migration[7.0] and defines a change method adding a column.
Step 2: Understand the add_column method
It adds an integer column 'age' to the existing 'users' table.
Final Answer:
Adds an integer column named 'age' to the 'users' table -> Option D
Quick Check:
add_column adds column = Adds an integer column named 'age' to the 'users' table [OK]
Hint: add_column adds a new column to existing table [OK]
Common Mistakes:
Confusing add_column with remove_column
Thinking it creates a new table
Missing migration version causes error (not here)
4. You wrote this migration:
class AddEmailToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :email
end
end
What is the problem when running rails db:migrate?
medium
A. Syntax error due to missing 'def up' method
B. Missing column type for 'email' causes an error
C. Migration runs successfully without issues
D. Table 'users' does not exist error
Solution
Step 1: Check add_column method usage
add_column requires three arguments: table name, column name, and column type.
Step 2: Identify missing argument
The migration misses the column type for 'email', causing an error.
Final Answer:
Missing column type for 'email' causes an error -> Option B
Quick Check:
add_column needs type argument [OK]
Hint: Always specify column type with add_column [OK]
Common Mistakes:
Omitting column type argument
Assuming migration runs without errors
Confusing change method with up/down methods
5. You want to create a migration that renames the column username to login in the accounts table. Which migration code correctly does this?
hard
A. class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
rename_column :accounts, :username, :login
end
end
B. class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
change_column :accounts, :username, :login
end
end
C. class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
rename_table :username, :login
end
end
D. class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
remove_column :accounts, :username
add_column :accounts, :login, :string
end
end
Solution
Step 1: Identify the correct method to rename a column
Rails provides rename_column to rename columns in a table.
Step 2: Check each option's method
class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
rename_column :accounts, :username, :login
end
end uses rename_column correctly; class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
change_column :accounts, :username, :login
end
end wrongly uses change_column; class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
rename_table :username, :login
end
end tries to rename tables; class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
remove_column :accounts, :username
add_column :accounts, :login, :string
end
end removes and adds columns instead of renaming.
Final Answer:
Use rename_column :accounts, :username, :login in change method -> Option A
Quick Check:
rename_column renames columns = class RenameUsernameToLogin < ActiveRecord::Migration[7.0]
def change
rename_column :accounts, :username, :login
end
end [OK]
Hint: Use rename_column to rename columns, not change_column [OK]
Common Mistakes:
Using change_column instead of rename_column
Trying to rename tables instead of columns
Removing and adding columns instead of renaming
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.