Skip to content

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

Description

@nadeemja

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}  

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions