What is eager loading vs lazy loading in Rails?

Answer

Lazy loading loads associated records only when they are accessed, triggering a separate SQL query each time. This causes the N+1 query problem: fetching 100 posts and accessing post.author on each fires 101 queries (1 for posts + 100 for authors). Eager loading with includes pre-loads associations upfront: Post.includes(:author).all generates at most 2 queries regardless of how many posts exist. Use eager_load for a single JOIN query or preload for separate queries. Always eager-load associations used in views to avoid N+1 issues.