Challenge - 5 Problems
Column Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Identify the correct syntax for adding a string column with a default value
Which option correctly adds a
name column of type string with a default value of 'guest' in a Rails migration?Ruby on Rails
class AddNameToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :name, :string, default: 'guest' end end
Attempts:
2 left
💡 Hint
Look for the correct keyword argument syntax for default values in Ruby.
✗ Incorrect
In Rails migrations, the default value for a column is set using the keyword argument
default:. Option B uses the correct syntax. Option B uses an invalid keyword default_value. Option B uses an incorrect hash rocket syntax without a symbol key. Option B uses a hash rocket but the symbol is passed as a symbol argument instead of a keyword argument, which is invalid here.❓ component_behavior
intermediate1:30remaining
What is the effect of
null: false on a column?In a Rails migration, what does adding
null: false to a column definition do?Ruby on Rails
add_column :users, :email, :string, null: false
Attempts:
2 left
💡 Hint
Think about database constraints and what
null: false means.✗ Incorrect
The
null: false option adds a database-level constraint that disallows NULL values in that column. This means the database will reject any record where this column is NULL. It does not set a default value or affect Rails validations directly.❓ state_output
advanced1:30remaining
What is the default limit for a
string column in Rails migrations?When you create a
string column in a Rails migration without specifying a limit, what is the default maximum length in the database?Ruby on Rails
add_column :users, :username, :string
Attempts:
2 left
💡 Hint
Consider common database defaults for VARCHAR columns.
✗ Incorrect
Rails maps the
string type to a VARCHAR(255) column by default in most SQL databases. This means the maximum length is 255 characters unless you specify a different limit.🔧 Debug
advanced2:00remaining
Why does this migration raise an error?
Given the migration code below, why does it raise an error when run?
Ruby on Rails
class AddAgeToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :age, :integer, limit: 10 end end
Attempts:
2 left
💡 Hint
Check the valid byte sizes for integer limits in Rails migrations.
✗ Incorrect
In Rails, the
limit option for integer columns specifies the number of bytes and must be one of 1, 2, 3, 4, or 8. Using 10 is invalid and causes an error. The limit option is valid for integers, the column name 'age' is allowed, and default values are optional.🧠 Conceptual
expert2:30remaining
How does Rails handle
decimal column precision and scale?In a Rails migration, when defining a
decimal column with precision and scale, what do these two attributes control?Ruby on Rails
add_column :products, :price, :decimal, precision: 8, scale: 2
Attempts:
2 left
💡 Hint
Think about how numbers with decimals are stored in databases.
✗ Incorrect
In Rails migrations,
precision defines the total number of digits that can be stored, including both sides of the decimal point. scale defines how many digits can be stored after the decimal point. For example, precision 8 and scale 2 means up to 6 digits before and 2 digits after the decimal.