elight / acts_as_commentable_with_threading

Similar to acts_as_commentable; however, utilizes awesome_nested_set to provide threaded comments
MIT License
671 stars 168 forks source link

Bullet gem complains about n+1 when using Model.root_comments and recursive partials #90

Open nadeemja opened 9 years ago

nadeemja commented 9 years ago

Hi,

The Bullet gem gives me the following error:

Comment => [:children]
  Add to your finder: :include => [:children]
N+1 Query method call stack
  /Users/Nadeem/rails/iQue/app/models/comment.rb:46:in `has_children?'
  /Users/Nadeem/rails/iQue/app/views/questions/_comment.html.haml:7:in `_app_views_questions__comment_html_haml___3048192702811821479_70228511044500'
  /Users/Nadeem/rails/iQue/app/views/questions/show.html.haml:22:in `block in _app_views_questions_show_html_haml___3264554324951737521_70228577037320'
  /Users/Nadeem/rails/iQue/app/views/questions/show.html.haml:21:in `_app_views_questions_show_html_haml___3264554324951737521_70228577037320'

How do we fix this?

Here is my controller code:

@comments = @question.root_comments

Here is my view code:

- @comments.each do |c|
  = render partial: 'comment', locals: {c: c, level: 0}  

Here is my partial:

.row
  %div{class: "col-xs-#{12 - level} pull-right"}
    %li
      .card
        = c.body

- if c.has_children?
  - c.children.each do |child|
    = render partial: 'comment', locals: {c: child, level: level + 1}  
DustinFisher commented 9 years ago

I am using partials in a recursive manner like you are showing.

I am not sure why your getting that error without seeing your entire setup, but I did put together a walkthrough of my setup. Let me know if that helps figure out why your getting that error.