As for now I'm working on a blog application that has article/comment models connected via has_many/belongs_to associations. To create nested comments functionality I use ancestry gem. However, I would like to eager load all of the descendants of a comment. Is there any ideas of how to approach this? I tried using join and where but it seems like they produce n + 1 queries. Here is how I call the method to display them in the view.
<%= nested_comments_display comments.arrange(:order => :created_at) %>
And here is nested_comments_display method
def nested_comments_display(comments)
comments.map do |comment, sub_comments|
render(comment) + content_tag(:div,nested_comments_display(sub_comments),
:class => "nested_comment")
end.join.html_safe
end
I also use decent_exposure gem and my CommentsController looks like this
class CommentsController < ApplicationController
expose(:article)
expose(:comments, ancestor: :article)
expose(:comment, attributes: :comment_params)
....
end