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

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

Open
nadeemja opened this issue Jan 18, 2015 · 1 comment

Comments

@nadeemja
Copy link

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
Copy link

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.

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

2 participants