Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Last active December 17, 2017 10:46
Show Gist options
  • Save StevenJL/ff66a4a7297dfcdde4f6a6c941b46a49 to your computer and use it in GitHub Desktop.
Save StevenJL/ff66a4a7297dfcdde4f6a6c941b46a49 to your computer and use it in GitHub Desktop.
# Eager-load all users' posts with `includes`
users = User.includes(:posts)
users.each do |user|
user.posts
end
# This will only run 2 queries.
# SELECT * FROM users;
# SELECT * FROM posts WHERE posts.user_ids IN (1,2,3,4,....,n);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment