Last active
December 17, 2017 10:46
-
-
Save StevenJL/ff66a4a7297dfcdde4f6a6c941b46a49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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