0
0
Ruby on Railsframework~20 mins

Creating migrations in Ruby on Rails - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the effect of this migration code?
Consider this Rails migration code snippet:
class AddAgeToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :age, :integer, default: 18
  end
end

What 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
ACreates a new table called 'age' with integer type
BRemoves the 'age' column from 'users' table
CChanges the 'age' column type to string in 'users' table
DAdds an integer column 'age' to 'users' table with default value 18 for new records
Attempts:
2 left
💡 Hint
Think about what add_column does and the effect of default value.
📝 Syntax
intermediate
2: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?
A
create_table :products do |t|
  t.string :name
  t.decimal :price
end
B
create_table :products, id: false do |t|
  t.string :name
  t.decimal :price
end
C
create_table :products, primary_key: :product_id do |t|
  t.string :name
  t.decimal :price
end
D
create_table :products, id: true do |t|
  t.string :name
  t.decimal :price
end
Attempts:
2 left
💡 Hint
By default, create_table adds an id primary key unless id: false is specified.
🔧 Debug
advanced
2:00remaining
Why does this migration raise an error?
Given this migration:
class ChangePriceTypeInProducts < ActiveRecord::Migration[7.0]
  def change
    change_column :products, :price, :string
  end
end

Running 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
AThe :string type is invalid for change_column
Bchange_column cannot be used inside the change method because it is irreversible
CThe migration file is missing a version number in the class inheritance
DThe :price column does not exist in the products table
Attempts:
2 left
💡 Hint
Think about whether change_column can be reversed automatically.
state_output
advanced
2:00remaining
What is the state of the database after this migration?
After running this migration:
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

Which 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
AThe 'orders' table has no timestamps columns
BThe 'orders' table has a 'user' column storing user names
CThe 'orders' table has a 'user_id' column with a foreign key constraint to 'users' table
DThe 'orders' table has a 'user_id' column but no foreign key constraint
Attempts:
2 left
💡 Hint
t.references adds a column with _id suffix and foreign_key: true adds a constraint.
🧠 Conceptual
expert
2: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?
AThe 'status' column is removed from the 'tasks' table
BThe 'status' column remains but default value is removed
CThe 'status' column remains with default 'pending'
DThe rollback fails because default values cannot be removed
Attempts:
2 left
💡 Hint
Rollback reverses the migration changes, removing added columns.