What is the purpose of column types in Rails migrations?
Column types define the kind of data a database column will store, such as strings, integers, or dates. This helps Rails and the database know how to handle and store the data correctly.
Click to reveal answer
beginner
Name three common column types used in Rails migrations.
Common column types include string (for short text), integer (for whole numbers), and boolean (for true/false values).
Click to reveal answer
beginner
What does the null: false attribute do in a Rails migration column?
It ensures that the column cannot have empty (NULL) values. This means every record must have a value for this column.
Click to reveal answer
beginner
How does the default attribute work in Rails migrations?
The default attribute sets a value automatically for the column when a new record is created without specifying that column's value.
Click to reveal answer
beginner
Why would you use the timestamps method in a Rails migration?
It adds two columns, created_at and updated_at, which automatically track when a record is created and last updated.
Click to reveal answer
Which column type is best for storing a user's email address in Rails?
Aboolean
Binteger
Ctext
Dstring
✗ Incorrect
Email addresses are short text, so the string type is appropriate.
What does setting null: false on a column do?
ASets a default value
BAllows the column to be empty
CPrevents the column from being empty
DDeletes the column
✗ Incorrect
null: false means the column must have a value; it cannot be empty.
Which attribute sets a value automatically if none is provided when creating a record?
Anull
Bdefault
Cindex
Dunique
✗ Incorrect
The default attribute sets an automatic value if none is given.
What columns does t.timestamps add in a migration?
Acreated_at and updated_at
Bstart_date and end_date
Cid and uuid
Dname and email
✗ Incorrect
t.timestamps adds created_at and updated_at columns.
Which column type would you use to store true/false values?
Aboolean
Binteger
Cstring
Ddate
✗ Incorrect
Boolean type stores true or false values.
Explain how you would define a column for a user's age that must always have a value and defaults to 18 if not provided.
Think about the type of data, whether it can be empty, and what value to use if none is given.
You got /3 concepts.
Describe the purpose of the timestamps method in Rails migrations and why it is useful.
Consider what information about records is important to keep automatically.
You got /3 concepts.
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
Step 1: Understand column types for data
Boolean columns store true or false values, perfect for yes/no data.
Step 2: Compare with other types
String and text store text, integer stores numbers, so they don't fit true/false data.
Final Answer:
boolean -> Option A
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
Step 1: Identify correct column type and options
We want a string type with null: false and default: 'Untitled'.
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.
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
Step 1: Identify null constraint
Currently null: true allows price to be null, which is not desired.
Step 2: Fix null constraint
Changing null: true to null: false ensures price cannot be null.
Final Answer:
Change null: true to null: false -> Option B
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?