Practice
1. What is the main purpose of creating an index in a Rails database?
easy
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 BQuick 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
Solution
Step 1: Recall Rails migration syntax for adding index
The correct method isadd_indexwith the table name as a symbol and the column name as a symbol.Step 2: Check each option's syntax
add_index :users, :emailuses correct symbols and method name. The other options have syntax errors or wrong method names like missing symbols, camelCase, or incorrect method.Final Answer:
add_index :users, :email -> Option AQuick Check:
Correct Rails syntax = add_index :table, :column [OK]
Hint: Use symbols and method name exactly: add_index :table, :column [OK]
Common Mistakes:
- Omitting colons for symbols
- Using wrong method names like addIndex
- Passing table and column without symbols
3. Given this migration code:
What will be the effect of this index?
add_index :orders, :user_id, unique: true
What will be the effect of this index?
medium
Solution
Step 1: Understand the unique option in add_index
Addingunique: truecreates a unique index that prevents duplicate values in the indexed column.Step 2: Analyze the effect on user_id in orders
This means no two orders can have the same user_id, so duplicates are prevented.Final Answer:
It prevents duplicate user_id values in orders -> Option DQuick Check:
unique: true = no duplicates allowed [OK]
Hint: unique: true means no duplicate values allowed [OK]
Common Mistakes:
- Thinking unique allows duplicates
- Confusing unique with non-unique index
- Believing it deletes duplicates automatically
4. You wrote this migration code:
But running migration gives an error. What is the likely problem?
add_index :products, :name, unique: true, name: 'index_products_on_name_which_is_too_long_for_postgresql_63_char_limit'
But running migration gives an error. What is the likely problem?
medium
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
Thenameoption is valid inadd_index, parentheses are optional in Ruby, andindex_nameis not a valid option.Final Answer:
The index name is too long -> Option AQuick 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
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
Settingunique: trueensures no duplicate combinations of first_name and last_name.Step 3: Evaluate options
add_index :employees, [:first_name, :last_name], unique: truecorrectly 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 CQuick 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
