rails-n-plus-one

Scan Ruby on Rails code for N+1 queries and suggest eager loading methods.

Updated Dec 8, 2022
One-click install
npx skills add https://github.com/lekemula/dotfiles --skill rails-n-plus-one
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-n-plus-one
Source: https://github.com/lekemula/dotfiles/tree/main/claude/skills/rails-n-plus-one
Command: npx skills add https://github.com/lekemula/dotfiles --skill rails-n-plus-one

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill identifies and helps resolve inefficient N+1 query patterns in Ruby on Rails applications, which can significantly degrade performance.

Core Features & Use Cases

  • N+1 Query Detection: Scans Ruby code (models, controllers, views, etc.) for common N+1 query anti-patterns.
  • Eager Loading Suggestions: Recommends appropriate eager loading techniques (includes, preload, eager_load) to optimize database queries.
  • Code Fixes: Provides before-and-after code snippets to illustrate the suggested optimizations.
  • Use Case: A developer suspects their application's performance is suffering due to database inefficiencies. They use this Skill to pinpoint and fix N+1 queries in their codebase.

Quick Start

Scan the file '/app/controllers/posts_controller.rb' for N+1 query problems.

Frequently Asked Questions about rails-n-plus-one

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I find and fix N+1 query problems in a Ruby on Rails application?

Scanning your Ruby on Rails application code detects N+1 query problems across models, controllers, and views, suggesting specific eager loading methods like `includes`, `preload`, or `eager_load` to optimize database queries.

What is the difference between includes, preload, and eager_load in Rails eager loading?

Rails eager loading distinguishes between `includes` (flexible delegation), `preload` (separate queries), and `eager_load` (single JOIN), and this analysis recommends the specific method needed to resolve your detected N+1 query issues.

How do I scan specific files like controllers or serializers for N+1 queries?

You can scan specific files like controllers, serializers, views, and service objects for N+1 queries by targeting the file path, which analyzes the code for inefficient data access patterns and suggests code fixes with before-and-after snippets.

When should I use strict_loading to prevent N+1 queries in Rails?

Use `strict_loading` in Rails to enforce eager loading and prevent N+1 queries by raising an error on lazy associations, a technique recommended alongside `includes` and `preload` when scanning detects inefficient data access patterns.

Can I detect N+1 query anti-patterns in Rails service objects and views?

Yes, N+1 query detection scans Rails service objects, views, models, controllers, and serializers to pinpoint inefficient data access patterns and provides specific eager loading recommendations to resolve them.