0
0
Ruby on Railsframework~20 mins

Changing column types 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 changing a column type in Rails migration
Which option correctly changes the column type of age from integer to string in a Rails migration?
Ruby on Rails
class ChangeAgeTypeInUsers < ActiveRecord::Migration[7.0]
  def change
    # change column type here
  end
end
Achange_column :age, :users, :string
Bchange_column :users, :age, :string
Cchange_column_type :age, :users, :string
Dchange_column_type :users, :age, :string
Attempts:
2 left
💡 Hint
Remember the order: table name, column name, then new type.
component_behavior
intermediate
2:00remaining
Effect of changing column type on existing data
What happens to existing data in the price column when you change its type from integer to string using change_column in Rails migration?
AExisting integer values are converted to strings preserving their values
BExisting data is deleted and the column is emptied
CMigration fails with a type mismatch error
DExisting data remains as integers and causes runtime errors
Attempts:
2 left
💡 Hint
Think about how databases handle type casting during column changes.
🔧 Debug
advanced
2:00remaining
Identify the error in this migration changing column type
What error will this migration raise when run?

class ChangeActiveToBooleanInUsers < ActiveRecord::Migration[7.0]
  def change
    change_column :users, :active, :boolean, default: false
  end
end
Ruby on Rails
class ChangeActiveToBooleanInUsers < ActiveRecord::Migration[7.0]
  def change
    change_column :users, :active, :boolean, default: false
  end
end
AActiveRecord::StatementInvalid: invalid type for default value
BArgumentError: unknown keyword: default
CSyntaxError: unexpected keyword_end
DNo error, migration runs successfully
Attempts:
2 left
💡 Hint
Check the allowed options for change_column method.
state_output
advanced
2:00remaining
Resulting schema after changing column type with null constraint
After running this migration, what will be the null constraint on the email column?

class ChangeEmailTypeInUsers < ActiveRecord::Migration[7.0]
  def change
    change_column :users, :email, :text, null: false
  end
end
Ruby on Rails
class ChangeEmailTypeInUsers < ActiveRecord::Migration[7.0]
  def change
    change_column :users, :email, :text, null: false
  end
end
AThe <code>email</code> column will NOT allow null values
BThe <code>email</code> column will allow null values
CThe migration will fail with an error about null constraint
DThe null constraint remains unchanged from before migration
Attempts:
2 left
💡 Hint
Does change_column support changing null constraints directly?
🧠 Conceptual
expert
3:00remaining
Best practice for changing column type with data migration
You need to change a column type from string to integer but existing data may not convert cleanly. What is the best approach to handle this safely in Rails migrations?
ADrop the table and recreate it with the new column type
BUse <code>change_column</code> directly and fix data errors later
CCreate a new column with the new type, migrate data manually, then remove old column
DChange the column type in the database manually outside Rails
Attempts:
2 left
💡 Hint
Think about data safety and rollback ability.