0
0
Ruby on Railsframework~20 mins

Column types and attributes in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Column Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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
Aadd_column :users, :name, :string, default => 'guest'
Badd_column :users, :name, :string, default: 'guest'
Cadd_column :users, :name, :string, default_value: 'guest'
Dadd_column :users, :name, :string, :default => 'guest'
Attempts:
2 left
💡 Hint
Look for the correct keyword argument syntax for default values in Ruby.
component_behavior
intermediate
1: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
AMakes the column optional in Rails validations
BAllows the column to accept NULL values
CSets the default value of the column to false
DPrevents the column from accepting NULL values in the database
Attempts:
2 left
💡 Hint
Think about database constraints and what null: false means.
state_output
advanced
1: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
A65,535 characters
BNo limit
C255 characters
D191 characters
Attempts:
2 left
💡 Hint
Consider common database defaults for VARCHAR columns.
🔧 Debug
advanced
2: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
AThe limit option for integer columns must be 1, 2, 3, 4, or 8 bytes; 10 is invalid
BThe column name 'age' is reserved and cannot be used
CThe migration is missing a default value for the age column
DThe limit option is only valid for string columns, not integers
Attempts:
2 left
💡 Hint
Check the valid byte sizes for integer limits in Rails migrations.
🧠 Conceptual
expert
2: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
A<code>precision</code> is total digits stored; <code>scale</code> is digits after the decimal point
B<code>precision</code> is digits after the decimal; <code>scale</code> is total digits stored
C<code>precision</code> sets the maximum value; <code>scale</code> sets the minimum value
D<code>precision</code> and <code>scale</code> are synonyms and control total digits
Attempts:
2 left
💡 Hint
Think about how numbers with decimals are stored in databases.