Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
249: Building a Recipe Sharing Platform API
I've seen this happen a dozen times with developers moving from small scripts to full-blown APIs. You build a feature, it works perfectly in your local environment with five test recipes, and then you deploy it. Suddenly, the production logs are screaming, and the API response time climbs from 100ms to 3 seconds. Usually, the culprit is exactly what I'm about to show you.
# app/controllers/api/v1/recipes_controller.rb
module Api
module V1
class RecipesController < ApplicationController
def index
@recipes = Recipe.all
# We want to return each recipe and its list of ingredients
render json: @recipes.map { |recipe|
{
title: recipe.title,
ingredients: recipe.ingredients.map(&:name)
}
}
end
end
end
end
The Log-Spamming N+1 Query
At first glance, this code is clean. It does exactly what the requirements ask: it fetches recipes and lists their ingredients. But if you look at your server logs while hitting this endpoint, you'll see something terrifying. You'll see one query to fetch all recipes, followed by a separate query for every single recipe to fetch its ingredients.
If you have 100 recipes, Ruby is making 101 database calls. We call this the N+1 problem. The "1" is the initial fetch for the recipes, and the "N" is the number of recipes that trigger additional queries. In a production environment with thousands of recipes, this will effectively DDOS your own database.
Eager Loading the Ingredients
The fix is a simple change to how we fetch the initial collection. We need to tell ActiveRecord to grab all the ingredients for all the recipes in one go, rather than waiting until the loop hits them. We do this using .includes.
# app/controllers/api/v1/recipes_controller.rb
module Api
module V1
class RecipesController < ApplicationController
def index
# This is the magic line. It tells Rails to "eager load" the ingredients.
@recipes = Recipe.includes(:ingredients).all
render json: @recipes.map { |recipe|
{
title: recipe.title,
ingredients: recipe.ingredients.map(&:name)
}
}
end
end
end
end
By adding .includes(:ingredients), Rails changes its strategy. Instead of 101 queries, it now performs two: one to get all the recipes, and one massive query to get all ingredients associated with those specific recipe IDs. It then maps them together in memory. I personally always check my logs during development using a gem like bullet, which will actually throw a notification at you the moment it detects an N+1 query. It's a lifesaver because it's so easy to forget this when you're focused on the JSON structure rather than the database layer.
📋 Practical Task
Implementing Dietary Restriction Filtering for the Recipe API
Now that we've optimized the base index action, we need to make the API actually useful for users with dietary needs. Your task is to modify the RecipesController#index action to support an optional category query parameter (e.g., /api/v1/recipes?category=vegan).
- Modify the
indexaction to check ifparams[:category]is present. - If the parameter exists, filter the recipes to only return those belonging to that category. Assume the
Recipemodel has acategorycolumn. - Ensure that you maintain the
.includes(:ingredients)optimization so that the filtered results don't trigger N+1 queries. - If no category is provided, the API should continue to return all recipes as it did before.
There are no comments for now.