0
0
Ruby on Railsframework~30 mins

Index creation in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Creating Indexes in Rails Migrations
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Check your class structure and indentation carefully.