Using Rails polymorphism for nested comments

I need to create a system of nested comments in the Rails 3 application, which allows you to leave comments on many models (articles, posts, etc.) and discuss a consolidated own solution according to this post . There are gems such as acts_as_commentable_with_threading with awesome_nested_set , but they feel bloated for my needs.

  • I need to be able to add comments to several models.
  • I need to be able to add comments to comments, infinitely deep
  • I need to be able to efficiently retrieve all descendants for publication, article, etc.
  • I need to be able to effectively submit comments in their respective nesting.

My question is, should I roll my decision, what potential hiccups can I encounter. I want not to follow the same path to reach a dead end. My initial concerns relate to an effective survey of children. Say, for example, getting a list of comments on descendants (children and children of children).

Does anyone have input? Thank.

+2
source share
3 answers

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

acts_as_tree parent_id, , , . , .

awesome_nested_set : parent_id, lft rgt. , . , .

, awesome_nested_set . , , . .

: Comment.roots , 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. , , .

. , awesome_nested_set - . ancestry. .

+6

, , - , , , - , - , .

CTE - , .

+1

- , :

attach_to_controller ()

attach_to_id (int)

AJAX .

, .

0
source

Source: https://habr.com/ru/post/1793519/


All Articles