惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
B
Blog
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
量子位
博客园 - 叶小钗
T
Tailwind CSS Blog
小众软件
小众软件
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
雷峰网
雷峰网
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
V
V2EX
博客园_首页
I
InfoQ
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
includes vs joins vs preload vs eager_load in Rails
Hassan Farooq · 2026-06-23 · via DEV Community
Cover image for includes vs joins vs preload vs eager_load in Rails

Hassan Farooq

These four all deal with associations, and they're easy to mix up. The way I keep them straight is to ask two questions about each: what SQL does it run, and does it actually pull the associated records into memory? Once you can answer both, picking the right one is straightforward.

Method SQL it generates Loads the association? Reach for it when
joins INNER JOIN No You filter or sort by the associated table
preload Separate queries, one per association Yes You'll read the association and want it loaded separately
eager_load A single LEFT OUTER JOIN Yes You need to read and filter by the association in one query
includes Rails picks preload or eager_load Yes The default for killing N+1 when you're not sure which you need

joins

joins builds a SQL join but does not load anything into memory. User.joins(:posts).where(posts: { published: true }) filters users by their posts cheaply. But if you then call user.posts in Ruby, you get a fresh query for every user, which is the N+1 you were trying to avoid, because the posts were never loaded. Use joins to filter and sort, not to display.

preload

preload always runs separate queries, one for the users and one for their posts, then stitches them together in memory. There is no join, so you can't reference the posts table in a where. Try it and Rails will complain.

eager_load

eager_load forces a single LEFT OUTER JOIN and builds the association out of the joined rows. Unlike preload, it still works when you filter on the joined table, because the join is right there in the query.

includes

includes is the one I reach for most. You're telling Rails what you want ("load these associations, don't N+1 me") and letting it choose the strategy. By default it preloads with separate queries. The moment you reference the associated table in a where or order, it switches to eager_load and runs the join instead. You usually don't have to think about which.

Putting it to work

Two quick cases to make it stick.

To list users along with their posts, use User.includes(:posts). You're displaying the association, so separate queries are fine and you've killed the N+1.

To find only the users who have published posts, use User.joins(:posts).where(posts: { published: true }). You're filtering by the association and you never need the post objects in memory, so there's no reason to load them. If you want to filter and then display those posts, use includes together with references, or just use eager_load.