📖 Scenario: You are building a simple Rails application to manage a library's book collection. Each book has a title, author, number of pages, and a flag indicating if it is currently available for borrowing.
🎯 Goal: Create a Rails migration to define a books table with appropriate column types and attributes for the book's title, author, pages, and availability.
📋 What You'll Learn
Create a migration file defining a books table
Add a title column as a string
Add an author column as a string
Add a pages column as an integer
Add an available column as a boolean with a default value of true
💡 Why This Matters
🌍 Real World
Defining database tables with correct column types and attributes is essential for storing and managing data in Rails applications.
💼 Career
Rails developers frequently write migrations to create and modify database schemas, ensuring data integrity and application functionality.
Progress0 / 4 steps
1
Create the books table
Write a migration class called CreateBooks inheriting from ActiveRecord::Migration[7.0]. Inside the change method, create a table named books.
Ruby on Rails
Hint
Use create_table :books do |t| ... end inside the change method.
2
Add title and author columns
Inside the create_table :books block, add a title column and an author column, both with the type string.
Ruby on Rails
Hint
Use t.string :title and t.string :author inside the block.
3
Add pages and available columns
Add a pages column with type integer and an available column with type boolean and a default value of true inside the create_table block.
Ruby on Rails
Hint
Use t.integer :pages and t.boolean :available, default: true.
4
Add timestamps
Add timestamps to the books table inside the create_table block to track creation and update times.
Ruby on Rails
Hint
Use t.timestamps inside the block to add created_at and updated_at columns.
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?