📖 Scenario: You are building a Rails application to manage a library. The database has a table called books with columns for title, author, and published_year. To make searching faster, you want to add an index on the author column.
🎯 Goal: Learn how to create a database index on a column using a Rails migration to speed up queries on the author field.
📋 What You'll Learn
Create a migration file to add an index on the author column of the books table
Define a variable for the table name
Use the add_index method in the migration
Complete the migration class with the change method
💡 Why This Matters
🌍 Real World
Indexes speed up database searches, making apps faster and more responsive.
💼 Career
Knowing how to create indexes in Rails migrations is essential for backend developers working with databases.
Progress0 / 4 steps
1
Create a migration class named AddIndexToBooksAuthor
Create a migration class called AddIndexToBooksAuthor that inherits from ActiveRecord::Migration[7.0].
Ruby on Rails
Hint
Start by defining the migration class with the correct name and inheritance.
2
Define a variable table_name with value :books
Inside the AddIndexToBooksAuthor class, define a variable called table_name and set it to the symbol :books.
Ruby on Rails
Hint
Use a constant inside the class to set TABLE_NAME.
3
Add the change method with add_index on author
Add a change method inside the AddIndexToBooksAuthor class. Inside it, call add_index with TABLE_NAME and the column :author.
Ruby on Rails
Hint
The change method is where you add the index using add_index.
4
Complete the migration class with proper indentation and syntax
Ensure the migration class AddIndexToBooksAuthor has the TABLE_NAME variable, the change method, and the add_index call with correct Ruby syntax and indentation.
Ruby on Rails
Hint
Check your class structure and indentation carefully.
Practice
(1/5)
1. What is the main purpose of creating an index in a Rails database?
easy
A. To store more data in the database
B. To speed up data searches by creating shortcuts
C. To delete duplicate records automatically
D. To backup the database regularly
Solution
Step 1: Understand what an index does
An index helps the database find data faster by creating a shortcut to the data location.
Step 2: Compare options with the purpose
Only To speed up data searches by creating shortcuts correctly describes speeding up data searches, while others describe unrelated tasks.
Final Answer:
To speed up data searches by creating shortcuts -> Option B
Quick Check:
Index = Faster search [OK]
Hint: Indexes speed up searches by creating shortcuts [OK]
Common Mistakes:
Thinking indexes store more data
Confusing indexes with backups
Believing indexes delete duplicates automatically
2. Which of the following is the correct syntax to add an index on the email column of the users table in a Rails migration?
easy
A. add_index :users, :email
B. add_index users, email
C. addIndex(:users, :email)
D. index_add :users, :email
Solution
Step 1: Recall Rails migration syntax for adding index
The correct method is add_index with the table name as a symbol and the column name as a symbol.
Step 2: Check each option's syntax
add_index :users, :email uses correct symbols and method name. The other options have syntax errors or wrong method names like missing symbols, camelCase, or incorrect method.
But running migration gives an error. What is the likely problem?
medium
A. The index name is too long
B. The name option is not allowed in add_index
C. The syntax is missing parentheses
D. The name option should be index_name
Solution
Step 1: Understand index name length limits
Database systems like PostgreSQL limit index name length (usually 63 characters). A too long name causes errors.
Step 2: Check the given index name
The name 'index_products_on_name_which_is_too_long_for_postgresql_63_char_limit' exceeds the database's maximum identifier length (typically 63 characters in PostgreSQL), causing the migration to fail.
Step 3: Confirm other options
The name option is valid in add_index, parentheses are optional in Ruby, and index_name is not a valid option.
Final Answer:
The index name is too long -> Option A
Quick Check:
Index name length limit causes errors [OK]
Hint: Check index name length if migration errors occur [OK]
Common Mistakes:
Thinking name option is invalid
Adding unnecessary parentheses
Using wrong option names like index_name
5. You want to create a composite unique index on first_name and last_name columns in the employees table to prevent duplicate full names. Which migration code is correct?
hard
A. add_index :employees, first_name, last_name, unique: true
B. add_index :employees, :first_name, :last_name, unique: true
C. add_index :employees, [:first_name, :last_name], unique: true
D. add_index :employees, [:first_name, :last_name], unique: false
Solution
Step 1: Understand composite index syntax
To create an index on multiple columns, pass an array of symbols for those columns.
Step 2: Check uniqueness requirement
Setting unique: true ensures no duplicate combinations of first_name and last_name.
Step 3: Evaluate options
add_index :employees, [:first_name, :last_name], unique: true correctly uses an array and unique true. The other options have wrong argument formats (separate arguments instead of array, missing symbols) or set unique: false, which does not prevent duplicates.
Final Answer:
add_index :employees, [:first_name, :last_name], unique: true -> Option C
Quick Check:
Composite unique index = array + unique: true [OK]
Hint: Use array for multiple columns and unique: true for uniqueness [OK]
Common Mistakes:
Passing columns as separate arguments instead of array
Forgetting unique: true for uniqueness
Using unique: false when uniqueness is needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.