Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to solve n+1 comments queries ? #4

Open
JamesAndresCM opened this issue Dec 13, 2018 · 0 comments
Open

How to solve n+1 comments queries ? #4

JamesAndresCM opened this issue Dec 13, 2018 · 0 comments

Comments

@JamesAndresCM
Copy link

Thank's for this work!
But i have a doubt, how to solve n+1 queries when render comments for post?

I try with include other models :

commentable.comments.includes(:user, :replies, :commentable).where(parent: nil)

in partial : _widget.html.erb

but the render _comment.html.erb
respond with :

 ↳ app/views/comments/_widget.html.erb:3
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 AND "comments"."parent_id" IS NULL  [["commentable_id", 27], ["commentable_type", "Post"]]
  ↳ app/views/comments/_widget.html.erb:7
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1  [["id", 24]]
  ↳ app/views/comments/_widget.html.erb:7
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" IN ($1, $2)  [["parent_id", 11], ["parent_id", 8]]
  ↳ app/views/comments/_widget.html.erb:7
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 24], ["LIMIT", 1]]
  ↳ app/views/comments/_comment.html.erb:8
  Post Load (0.5ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 ORDER BY "posts"."created_at" DESC LIMIT $2  [["id", 27], ["LIMIT", 1]]
  ↳ app/views/comments/_comment.html.erb:10
  Comment Exists (0.4ms)  SELECT  1 AS one FROM "comments" WHERE "comments"."parent_id" = $1 LIMIT $2  [["parent_id", 9], ["LIMIT", 1]]
  ↳ app/views/comments/_comment.html.erb:15
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" = $1  [["parent_id", 9]]
  ↳ app/views/comments/_comment.html.erb:17
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 24], ["LIMIT", 1]]
  ↳ app/views/comments/_comment.html.erb:8
  CACHE Post Load (0.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 ORDER BY "posts"."created_at" DESC LIMIT $2  [["id", 27], ["LIMIT", 1]]
  ↳ app/views/comments/_comment.html.erb:10
  Comment Exists (0.5ms)  SELECT  1 AS one FROM "comments" WHERE "comments"."parent_id" = $1 LIMIT $2  [["parent_id", 10], ["LIMIT", 1]]
  ↳ app/views/comments/_comment.html.erb:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant