<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q" -->

---
title: How to Fix N+1 Queries in Rails Like a Pro | daily.dev
description: N+1 queries are a common Rails performance killer where fetching associated records in a loop triggers hundreds of redundant database calls. The fix is eager...
canonical: https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Fix N+1 Queries in Rails Like a Pro | daily.dev
og:description: N+1 queries are a common Rails performance killer where fetching associated records in a loop triggers hundreds of redundant database calls. The fix is eager...
og:url: https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q
og:image: https://api.daily.dev/og/posts/bDtTOmg9Q.png
og:image:alt: How to Fix N+1 Queries in Rails Like a Pro
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Fix N+1 Queries in Rails Like a Pro

**[Ruby Flow](https://daily.dev/sources/rubyflow)** · 6 min read · 1 upvotes · 0 comments

## Summary

N+1 queries are a common Rails performance killer where fetching associated records in a loop triggers hundreds of redundant database calls. The fix is eager loading via ActiveRecord's `.includes` method, which pre-fetches all related data in one or two queries instead of one per record. For nested associations, pass a hash to `.includes`. Under the hood, Rails has three eager loading strategies: `.preload` (two separate queries), `.eager_load` (a single LEFT OUTER JOIN, required when filtering on the joined table), and `.includes` (smart default that picks between the two). To catch N+1 bugs before they reach production, enable `strict_loading_by_default` in development, which raises an error the moment a lazy-loaded association is accessed.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://norvilis.com/how-to-fix-n-1-queries-in-rails-like-a-pro>

## Similar posts on daily.dev

- [Speed Up Your Rails App by Squashing N\+1 Queries](https://daily.dev/posts/speed-up-your-rails-app-by-squashing-n-1-queries-ptt97gwbo) · Hashrocket · 0 upvotes · 0 comments
- [The N\+1 Query Problem in Active Record](https://daily.dev/posts/the-n-1-query-problem-in-active-record-yh0vscveu) · Ruby Flow · 0 upvotes · 0 comments
- [Why N\+1 Queries Are a Natural Result of Lazy Loading](https://daily.dev/posts/why-n-1-queries-are-a-natural-result-of-lazy-loading-b9boclvq7) · Ruby Flow · 1 upvotes · 0 comments

---

Tags: [#rails](https://daily.dev/tags/rails)

[View this post on daily.dev](https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Fix N+1 Queries in Rails Like a Pro","url":"https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q"},"datePublished":"2026-04-12T05:28:47.123Z","dateModified":"2026-04-12T05:29:11.700Z","description":"N+1 queries are a common Rails performance killer where fetching associated records in a loop triggers hundreds of redundant database calls. The fix is eager...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fc2940a0ffd4e7041367ad32a45b6dc5?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fc2940a0ffd4e7041367ad32a45b6dc5?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Ruby Flow","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Ruby Flow","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/a21aaa8bcbbf448193a016895c90a0e1","url":"https://daily.dev/sources/rubyflow"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-fix-n-1-queries-in-rails-like-a-pro-bdttomg9q","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"rails","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Ruby Flow","item":"https://daily.dev/sources/rubyflow"},{"@type":"ListItem","position":3,"name":"How to Fix N+1 Queries in Rails Like a Pro"}]}
```

