将 Rails 多态用于嵌套注释 [英] Using Rails polymorphism for nested comments

查看:18
本文介绍了将 Rails 多态用于嵌套注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Rails 3 应用程序中构建一个嵌套的评论系统,该系统允许对许多模型(文章、帖子等)发表评论,并且我正在讨论按照 这篇文章.有一些可用的宝石,例如 acts_as_commentable_with_threadingawesome_nested_set,但它们对我的需求感到臃肿.

I need to build a nested comments system in a Rails 3 application that allows for comments on many models (articles, posts, etc) and am debating rolling my own solution along the lines of this post. There are gems available like acts_as_commentable_with_threading with awesome_nested_set, but they feel bloated for my needs.

  1. 我需要能够向多个模型添加评论
  2. 我需要能够在评论中添加无限深的评论
  3. 我需要能够有效地检索帖子、文章等的所有后代
  4. 我需要能够在适当的嵌套中有效地呈现评论

我的问题是,我是否要推出自己的解决方案,我可能会遇到哪些潜在的问题.我想避免只走一条路就走到死胡同.我最初的担忧与有效查询儿童有关.比如说,获取一个文章后代评论的列表(孩子和孩子的孩子).

My question is, were I to roll my own solution what potential hiccups I could face. I want to avoid going down one path only to reach a dead end. My initial concerns relate to efficiently querying for children. Say, for instance, getting a list of an articles descendant comments (children and children of children).

有人对此有意见吗?谢谢.

Anyone have input on this? Thanks.

推荐答案

您可以执行两种嵌套:树和嵌套集.

There are two kinds of nesting you can do: a tree and a nested set.

acts_as_tree 仅存储一个 parent_id,因此写入新条目的速度非常快,但您必须递归遍历 id 编号链以获取所有条目的列表这些孩子.当您需要进行大量读取时,这不是一个好的选择.

acts_as_tree stores only a parent_id and so it is really fast to write new entries, but you have to recursively walk the chain of id numbers to get a list of all the children. This is not a good choice when you need to do lots of reads.

awesome_nested_set 记录了三位信息:parent_idlftrgt.计算 left 和 right 值,以便它们包含该条目的所有子 ID.这对于读取操作非常快,但写入速度较慢.

awesome_nested_set records three bits of information: parent_id, lft and rgt. The left and right values are calculated so that they contain all the children ids for that entry. This is very fast for read operations but slower to write.

在你的情况下,我认为 awesome_nested_set 更合适.您可能认为这似乎有点矫枉过正,但嵌套集很快就会变得复杂.您需要使用嵌套集模式来有效地查询子项.

In your case I think awesome_nested_set is more appropriate. You might think it seems overkill, but nested sets get complicated in a hurry. You need to use the nested set pattern to efficiently query children.

您只需要使用两种方法来渲染整个评论树:迭代 Comment.roots 并为每个评论渲染 comment.children.

You only need to use two methods to render the entire tree of comments: iterate over Comment.roots and for each comment, render comment.children.

class ModelController < ApplicationController
  def show
    @model = Model.find_by_id(params[:id])
    @comments = @model.comments.roots
  end
end

<ul id="comments">
<% @comments.each do |comment| %>
  <%= render :partial => 'comment', :object => comment %>
<% end %>
</ul>

<!-- _comment partial -->
<li class="comment">
  <!-- comment markup -->
  <% if comment.children.present? %>
  <ul>
    <%= render :partial => 'comment', :collection => comment.children %>
  </ul>
  <% end %>
</li>

要保存嵌套评论,只需填写 parent_id,然后 awesome_nested_set 将完成剩下的工作.我不认为推出自己的解决方案会比这更优雅.

To save a nested comment, simply fill in the parent_id and awesome_nested_set will do the rest. I don't think rolling your own solution will be any more elegant than this.

更新:看起来 awesome_nested_set 已经有一段时间没有更新了.请查看祖先.做基本相同的事情.

Update: Looks like the awesome_nested_set hasn't been updated in some time. Check out ancestry instead. Does basically the same things.

这篇关于将 Rails 多态用于嵌套注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆