Challenge - 5 Problems
Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the effect of this migration code?
Consider this Rails migration code snippet:
What will be the result after running this migration?
class AddAgeToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :age, :integer, default: 18
end
endWhat will be the result after running this migration?
Ruby on Rails
class AddAgeToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :age, :integer, default: 18 end end
Attempts:
2 left
💡 Hint
Think about what add_column does and the effect of default value.
✗ Incorrect
The migration adds a new integer column named 'age' to the existing 'users' table. The default: 18 means new records will have age set to 18 unless specified otherwise.
📝 Syntax
intermediate2:00remaining
Which migration code will create a table with a primary key?
You want to create a new table 'products' with columns 'name' (string) and 'price' (decimal). Which migration code correctly creates this table with an id primary key?
Attempts:
2 left
💡 Hint
By default, create_table adds an id primary key unless id: false is specified.
✗ Incorrect
Option A creates the table with the default primary key 'id'. Option A disables the id column. Option A tries to set a custom primary key but syntax is incorrect. Option A is valid but redundant because id: true is default and does not need to be specified.
🔧 Debug
advanced2:00remaining
Why does this migration raise an error?
Given this migration:
Running this migration raises an error. What is the cause?
class ChangePriceTypeInProducts < ActiveRecord::Migration[7.0]
def change
change_column :products, :price, :string
end
endRunning this migration raises an error. What is the cause?
Ruby on Rails
class ChangePriceTypeInProducts < ActiveRecord::Migration[7.0] def change change_column :products, :price, :string end end
Attempts:
2 left
💡 Hint
Think about whether change_column can be reversed automatically.
✗ Incorrect
change_column is not reversible by default, so it should be used inside up/down methods or with reversible blocks. Using it inside change causes an error.
❓ state_output
advanced2:00remaining
What is the state of the database after this migration?
After running this migration:
Which statement is true about the 'orders' table?
class CreateOrders < ActiveRecord::Migration[7.0]
def change
create_table :orders do |t|
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
endWhich statement is true about the 'orders' table?
Ruby on Rails
class CreateOrders < ActiveRecord::Migration[7.0] def change create_table :orders do |t| t.references :user, null: false, foreign_key: true t.timestamps end end end
Attempts:
2 left
💡 Hint
t.references adds a column with _id suffix and foreign_key: true adds a constraint.
✗ Incorrect
t.references :user adds a 'user_id' integer column. foreign_key: true adds a database foreign key constraint linking to users table. t.timestamps adds created_at and updated_at columns.
🧠 Conceptual
expert2:00remaining
What happens if you rollback a migration that adds a column with default value?
You run a migration that adds a column 'status' with default 'pending' to 'tasks' table. Then you rollback this migration. What is the state of the 'tasks' table after rollback?
Attempts:
2 left
💡 Hint
Rollback reverses the migration changes, removing added columns.
✗ Incorrect
Rolling back a migration that adds a column removes that column entirely, including any default values. The table returns to its previous state.