Use includes to load related data in one query and avoid extra database calls.
Use select to get only the fields you need, making queries faster.
Examples
Loads active users and their posts in one go to avoid extra queries when accessing posts.
Ruby on Rails
User.where(active: true).includes(:posts)
Gets only the post ID and title, sorted by newest first, which is faster than loading all fields.
Ruby on Rails
Post.select(:id, :title).order(created_at: :desc)
Finds comments where the user is active by joining tables in one query.
Ruby on Rails
Comment.joins(:user).where(users: {active: true})
Sample Program
This controller action loads all published articles and their authors in one database query. This avoids asking the database again for each article's author, making the page load faster.
Ruby on Rails
class ArticlesController < ApplicationController
def index
# Load articles with their authors to avoid extra queries
@articles = Article.includes(:author).where(published: true).order(published_at: :desc)
end
end
OutputSuccess
Important Notes
Always check your queries with Rails console or logs to see how many database calls happen.
Use includes for associations to prevent the N+1 query problem.
Be careful not to load too much data; select only what you need.
Summary
Optimizing queries makes your app faster and saves server resources.
Use includes to load related data efficiently.
Use select and where to get only needed data.
Practice
(1/5)
1. What is the main benefit of using includes in Rails queries?
easy
A. It loads associated records in fewer database queries to improve performance.
B. It deletes unused records from the database automatically.
C. It encrypts data before saving to the database.
D. It creates new database tables for associations.
Solution
Step 1: Understand what includes does
includes loads related records in one or fewer queries to avoid repeated database hits.
Step 2: Identify the benefit
This reduces the number of queries, speeding up the app and saving server resources.
Final Answer:
It loads associated records in fewer database queries to improve performance. -> Option A
Quick Check:
Using includes reduces queries = B [OK]
Hint: Think: fewer queries means faster loading [OK]
Common Mistakes:
Confusing includes with deleting records
Thinking includes encrypts data
Assuming includes creates tables
2. Which of the following is the correct syntax to select only the name and email columns from a User model in Rails?
easy
A. User.find(:name, :email)
B. User.where(:name, :email)
C. User.includes(:name, :email)
D. User.select(:name, :email)
Solution
Step 1: Recall the method to choose specific columns
The select method specifies which columns to retrieve from the database.
Step 2: Check the syntax
User.select(:name, :email) correctly selects only the name and email columns.
Final Answer:
User.select(:name, :email) -> Option D
Quick Check:
Select columns = select method = D [OK]
Hint: Use select to pick columns, not where or includes [OK]
A. The select excludes the primary key (:id), causing posts not to load; fix by including :id in select.
B. The includes method is used incorrectly; replace it with joins.
C. The where clause should come after select to filter correctly.
D. The query will delete inactive users; remove where clause.
Solution
Step 1: Understand select limits columns
The select(:name) limits columns returned, but the primary key id is missing.
Step 2: Effect on includes
Without the primary key, Rails cannot load associated posts properly with includes.
Step 3: Fix the query
Add the primary key column (e.g., :id) to the select list to allow loading posts.
Final Answer:
The select excludes the primary key (:id), causing posts not to load; fix by including :id in select. -> Option A
Quick Check:
Select must include primary key for includes to work = A [OK]
Hint: Always select primary keys when using includes [OK]
Common Mistakes:
Removing primary key from select breaks associations
Replacing includes with joins unnecessarily
Changing where order incorrectly
5. You want to optimize a query to fetch Order records with their Customer and only the total_price and created_at fields from orders. Which Rails query is best for this optimization?
hard
A. Order.joins(:customer).select('orders.total_price, orders.created_at, customers.*')
B. Order.includes(:customer).select(:total_price, :created_at, :customer_id)
C. Order.includes(:customer).select(:total_price, :created_at)
D. Order.all.select(:total_price, :created_at)
Solution
Step 1: Identify needed fields and associations
You want orders with only total_price and created_at, plus associated customers.
Step 2: Ensure foreign key is selected for includes
Using includes(:customer) requires selecting the foreign key customer_id to load customers efficiently.
Step 3: Choose the query that selects needed fields and includes association correctly
Order.includes(:customer).select(:total_price, :created_at, :customer_id) meets all requirements.
Final Answer:
Order.includes(:customer).select(:total_price, :created_at, :customer_id) -> Option B
Quick Check:
Select needed fields + foreign key + includes = A [OK]
Hint: Include foreign keys in select when using includes [OK]
Common Mistakes:
Omitting foreign key breaks includes
Using joins loads all customer fields unnecessarily
Selecting all orders without includes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.