我是Ruby on Rails的新手,我需要使用嵌套评论来创建发布/评论关系,就像作者可以相互回复一样。祖先,has_many_roots?
制造这样的:
后/ comments.html:
<% @post.comments.roots.each do |c| %>
<%= nested_messages c.subtree.arrange(:order => :created_at) %>
<% end %>
这工作得很好,但显然需要大量的查询来渲染一棵树,像N + 1,其中N是comments.root.count
。
感谢您的帮助!
UPD: Soluton与.includes()对我的情况下,没有工作,但我不是100%肯定,我是在做正确的事情都...
该解决方案为我工作了很明显 - 安排意见自理,通过指定POST_ID:
<%= nested_messages Comment.where('post_id = ?', @post.id).arrange(:order => :created_at) %>