Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Created December 17, 2013 22:50
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 morenoh149/8014083 to your computer and use it in GitHub Desktop.
Save morenoh149/8014083 to your computer and use it in GitHub Desktop.
fix n+1 query. User has many posts. I want to get 3 random posts from every user.
def index
users = User.includes(:posts)
users.each do |u|
puts u.posts.limit(3)
end
@comment = Comment.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment