Rails Queries, Hotwire and Background Jobs

Rails 7/8 fixes and features: N+1-free Active Record, Turbo Frames/Streams, idempotent Sidekiq jobs, and RSpec that guards them.

Development / Backendrailsactive-record
by AgentVoltv1.0.0
Free to sign up · every skill included with AgentVolt Pro

Before & after

Same prompt, same agent — the only difference is the skill.

Prompt“I need to optimize a Rails query that's hitting the database 50 times for one request.”
Beforegeneric AI agent
I can help you optimize that Rails query. Rails has built-in tools like includes and eager_load that load associated records in fewer database queries. You might have an N+1 query problem where you're loading a record and then loading its associations in a loop. You could also use select to only fetch the columns you need. Depending on what your query is doing, there might be other strategies — like adding indexes to your database, using a join instead of separate queries, or caching results. It would help to see the actual code and understand what data you're trying to fetch. Are you using Active Record, or are you writing raw SQL? Have you profiled the queries to see exactly which queries are running?
Afterwith Rails Queries, Hotwire and Background Jobs
N+1 QUERY ANALYSIS + FIX

PROBLEMATIC CODE (model: Post, has_many :comments)
   posts = Post.all
   posts.each do |post|
     post.comments.map(&:author)
   end
   Result: N+1: loads author for each comment

PROFILE: 1 initial query (posts) + 50 queries (1 per post for comments) = 51 queries

FIX 1: EAGER LOAD (simplest)
   posts = Post.includes(:comments).all
   posts.each do |post|
     post.comments.map(&:author)
   end
   
   Problem: Still N+1 for authors (loads all comment authors separately)
   New total: 2 queries (posts + comments loaded, but still separate author fetch)

FIX 2: DEEP EAGER LOAD (correct solution)
   posts = Post.includes(comments: :author).all
   
   Total queries: 3 (posts + comments + authors in one shot)
   Verify: Add .explain to confirm query plan

FIX 3: SELECT SPECIFIC COLUMNS (if you don't need all data)
   posts = Post.includes(:comments).select(:id, :title, :created_at)
   
   Benefit: Smaller payload, faster serialization

ACTIVE RECORD QUERY HINTS
   includes() → uses LEFT OUTER JOIN (fetches all, can be slow on huge joins)
   eager_load() → explicit JOIN (faster, used when filtering on association)
   preload() → separate queries (best when you don't need to filter on association)

PERFORMANCE MEASUREMENT
   Before: 51 queries, 800ms
   After (eager_load): 3 queries, 60ms
   Improvement: 13x faster

ADD INDEXES
   After queries are lean, add database index on Post.comments and Comment.author_id

RSPEC TEST (prevent regression)
   expect query count to not exceed 3 for the full operation

About this skill

Bullet keeps flagging N+1s that includes doesn't fix. Your Turbo form swallows validation errors, and broadcasts work in the console but never from your job. This skill diagnoses the actual Rails cause, gives you the exact diff, and adds a spec that fails if the bug comes back.

What it does

  • Picks the right loader for each query (preload, eager_load, or includes). It also catches the N+1s that preloading can't fix: .count, scoped associations, and methods called inside partials.
  • Fixes Turbo issues at the protocol level: 422 on failed submits, see_other redirects, matching frame ids, and breaking out of frames.
  • Makes broadcasts safe. They render after commit, carry no per-user content in shared streams, use scoped stream names, and run on a production-grade Action Cable adapter.
  • Makes Sidekiq and Active Job work idempotent and commit-safe: ids as args, state guards, atomic transitions, bounded retries, and pool sizing.
  • Writes guarding specs: query-count specs that compare small and large N, Turbo Stream request specs, run-twice idempotency specs, and broadcast assertions.
  • Flags migrations that will lock a large table and suggests concurrent indexes, ignored_columns, and batched backfills.

What you get

A short diagnosis, file-labeled code changes, a spec that fails before the fix, a verification command, and risk notes. Version-dependent behavior is marked for you to confirm.

Best for

Rails developers and small teams running Hotwire apps with Sidekiq or Active Job who need performance and correctness fixes they can merge, not a lecture.

SKILL.md preview

SKILL.md
---
name: rails-queries-hotwire-background-jobs

… (sign up to view the full skill)
Sign up to view, copy, and install the full skill

More development skills

View all Development skills →