0
0
Ruby on Railsframework~20 mins

Adding and removing columns in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct syntax for adding a column in Rails migration
Which option correctly adds a new column named age of type integer to the users table in a Rails migration?
Ruby on Rails
class AddAgeToUsers < ActiveRecord::Migration[7.0]
  def change
    # Add column here
  end
end
Aadd_column :users, :age, :integer
Badd_column :age, :users, :integer
Cadd_column :users, :age, integer
Dadd_column users, :age, :integer
Attempts:
2 left
💡 Hint
Remember the order: table name, column name, then type, all symbols except type must be symbols.
component_behavior
intermediate
2:00remaining
Effect of removing a column in Rails migration
After running this migration, what will happen to the email column in the customers table?
Ruby on Rails
class RemoveEmailFromCustomers < ActiveRecord::Migration[7.0]
  def change
    remove_column :customers, :email, :string
  end
end
AThe <code>email</code> column will be set to NULL but not removed.
BThe <code>email</code> column will be renamed to <code>string</code>.
CThe migration will fail because <code>remove_column</code> does not accept a type argument.
DThe <code>email</code> column will be deleted from the <code>customers</code> table.
Attempts:
2 left
💡 Hint
Think about what remove_column does in Rails migrations.
🔧 Debug
advanced
2:00remaining
Identify the error in this migration removing a column
What error will this migration raise when run?
Ruby on Rails
class RemoveStatusFromOrders < ActiveRecord::Migration[7.0]
  def change
    remove_column :orders, :status
  end
end
ANo error, migration runs successfully and removes the column.
BRuntimeError because remove_column requires a block.
CSyntaxError due to missing colon before :status.
DArgumentError because the column type is missing in remove_column call.
Attempts:
2 left
💡 Hint
Check the Rails documentation for remove_column method signature.
state_output
advanced
2:00remaining
State of the database after adding and removing columns
Given these two migrations run in order, what columns will the products table have?
Ruby on Rails
class AddDescriptionToProducts < ActiveRecord::Migration[7.0]
  def change
    add_column :products, :description, :text
  end
end

class RemovePriceFromProducts < ActiveRecord::Migration[7.0]
  def change
    remove_column :products, :price, :decimal
  end
end
AThe <code>products</code> table will have neither <code>description</code> nor <code>price</code> columns.
BThe <code>products</code> table will have both <code>description</code> and <code>price</code> columns.
CThe <code>products</code> table will have the <code>description</code> column added and the <code>price</code> column removed.
DThe migrations will fail because you cannot add and remove columns in separate migrations.
Attempts:
2 left
💡 Hint
Think about what each migration does and that migrations run in order.
🧠 Conceptual
expert
3:00remaining
Understanding reversible migrations with add_column and remove_column
Which statement best describes how Rails handles the change method when using add_column and remove_column in a migration?
ARails can reverse both <code>add_column</code> and <code>remove_column</code> automatically without any extra code.
BRails automatically knows how to reverse <code>add_column</code> by removing the column, but cannot reverse <code>remove_column</code> without extra information.
CRails cannot reverse either <code>add_column</code> or <code>remove_column</code> automatically; you must write separate <code>up</code> and <code>down</code> methods.
DRails reverses <code>remove_column</code> by adding the column back with default values, but cannot reverse <code>add_column</code>.
Attempts:
2 left
💡 Hint
Think about what information Rails needs to reverse a migration.