Skip to content Skip to sidebar Skip to footer

Tag: how to

How to Solve N+1 Query Issues in Ruby

query Issues in Ruby If you want to solve N+1 query issues in Ruby, you need to use the ActiveRecord#eager_load or ActiveRecord#includes methods. # without eager loading posts = Post.all posts.each do |post| puts post.user.name end # with eager loading posts = Post.includes(:user) posts.each do |post| puts post.user.name end The ActiveRecord#includes method will eagerly load the associations (in this case, the user association).…

Read more