0

So I've got a forum thread full of posts, and I want to eager load the users from the posts and those users roles, to reduce the total number of database queries (which seems to be the best way to do things)

(to make things extra fun, forum and posts are nested resources, if that makes a differnece)

def show
  @forum = Forum.find(params[:forum_id])
  @posts = @forum.posts.includes(:user => :role).where('id = ? OR parent_post_id =?',params[:id], params[:id])
end

The page renders, but I can't really tell by looking at the console if it's actually reducing the number of queries or not. The console appears to have a slight reduction in number of queries to the CACHE, but the Load calls holds steady. Am I doing this right?

DVG
  • 17,392
  • 7
  • 61
  • 88

1 Answers1

0

yes, I think so, if the eager loading is successful, the query count will be reduced. If you are using MYsql, I suggest you use "query_reviewer". ( https://github.com/dsboulder/query_reviewer) it will show your how many queries were executed for opening a page.

Siwei
  • 19,858
  • 7
  • 75
  • 95