Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which column type in Rails is best suited to store true or false values?
easy
A. boolean
B. string
C. integer
D. text

Solution

  1. Step 1: Understand column types for data

    Boolean columns store true or false values, perfect for yes/no data.
  2. Step 2: Compare with other types

    String and text store text, integer stores numbers, so they don't fit true/false data.
  3. Final Answer:

    boolean -> Option A
  4. Quick Check:

    Boolean = true/false [OK]
Hint: True/false data uses boolean type in Rails [OK]
Common Mistakes:
  • Choosing string or text for true/false data
  • Using integer to represent boolean values
  • Confusing text with boolean type
2. Which of the following is the correct syntax to add a non-nullable string column named title with a default value 'Untitled' in a Rails migration?
easy
A. add_column :books, :title, :text, null: false, default: 'Untitled'
B. add_column :books, :title, :string, default: 'Untitled', null: true
C. add_column :books, :title, :string, null: false, default: 'Untitled'
D. add_column :books, :title, :string, null: false

Solution

  1. Step 1: Identify correct column type and options

    We want a string type with null: false and default: 'Untitled'.
  2. Step 2: Check syntax order and options

    add_column :books, :title, :string, null: false, default: 'Untitled' correctly uses :string, null: false, and default: 'Untitled'. The option with null: true allows null values. The option with :text uses the wrong type. The option without default misses the default value.
  3. Final Answer:

    add_column :books, :title, :string, null: false, default: 'Untitled' -> Option C
  4. Quick Check:

    Correct syntax with null and default options [OK]
Hint: Use null: false and default: 'value' together in add_column [OK]
Common Mistakes:
  • Forgetting null: false for required columns
  • Using wrong column type like text instead of string
  • Omitting default value when needed
3. Given this migration snippet:
create_table :users do |t|
  t.integer :age, null: false, default: 18
  t.boolean :active, default: true
end

What will be the default value of active for a new user if not specified?
medium
A. true
B. false
C. nil
D. 0

Solution

  1. Step 1: Read the default value for active

    The migration sets default: true for the boolean column active.
  2. Step 2: Understand default behavior

    If no value is given when creating a user, active will be set to true automatically.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    Boolean default true means new records get true [OK]
Hint: Default value sets initial data if none provided [OK]
Common Mistakes:
  • Assuming default is false if not specified
  • Confusing nil with default value
  • Thinking integer 0 is default for boolean
4. You wrote this migration line:
add_column :orders, :price, :integer, null: true, default: 0

But you want to ensure price is always set and never null. What is the fix?
medium
A. Add null: true, default: nil
B. Change null: true to null: false
C. Change :integer to :float
D. Remove default: 0

Solution

  1. Step 1: Identify null constraint

    Currently null: true allows price to be null, which is not desired.
  2. Step 2: Fix null constraint

    Changing null: true to null: false ensures price cannot be null.
  3. Final Answer:

    Change null: true to null: false -> Option B
  4. Quick Check:

    Use null: false to prevent null values [OK]
Hint: null: false means column must have a value [OK]
Common Mistakes:
  • Leaving null: true when value must be required
  • Removing default without fixing null
  • Changing type without addressing null
5. You want to add a status column to your tasks table that stores short text but limits it to 10 characters, defaults to 'pending', and cannot be null. Which migration line is correct?
hard
A. add_column :tasks, :status, :string, limit: 255, null: false, default: 'pending'
B. add_column :tasks, :status, :text, null: false, default: 'pending'
C. add_column :tasks, :status, :string, null: true, default: 'pending'
D. add_column :tasks, :status, :string, limit: 10, null: false, default: 'pending'

Solution

  1. Step 1: Choose correct column type and limit

    Use :string with limit: 10 to restrict text length to 10 characters.
  2. Step 2: Set null and default options

    Set null: false to require a value and default: 'pending' for initial value.
  3. Final Answer:

    add_column :tasks, :status, :string, limit: 10, null: false, default: 'pending' -> Option D
  4. Quick Check:

    String with limit, null false, and default set [OK]
Hint: Use limit to restrict string length in migrations [OK]
Common Mistakes:
  • Using text type which ignores limit
  • Allowing null when value is required
  • Setting wrong limit size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard