Skip to content

Instantly share code, notes, and snippets.

@Bajena
Created January 27, 2019 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bajena/f58850727f8aeeb107fc24d68cbffef9 to your computer and use it in GitHub Desktop.
Save Bajena/f58850727f8aeeb107fc24d68cbffef9 to your computer and use it in GitHub Desktop.
Rails includes
posts = Post.where(id: [1, 2, 3]).includes(:user)
# SELECT * FROM posts WHERE id IN (1, 2, 3)
# SELECT * FROM users WHERE id IN (1, 2, 3)
users = posts.map { |post| post.user }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment