Search code examples
ruby-on-railsthreaded-comments

Is there native support in Rails or Ruby for representing threaded comments


I want to have a comments section in my app that looks like this:

response1
 response1a
 response1b
  response1b1
response2
 response2a
 response2b
 response2c
  response2c1
   response2c1a
    response2c1a1
     response2c1a1
      response2c1a1a
       response2c1a1a1

Assuming I do this by using HTML such as the following:

<div class="comment">
  Response1
  <div class="comment">
    Response1a
    <div class="comment">
      Response1a1
    </div>
  </div>
  <div class="comment">
    Response1b
  </div>
</div>

And this CSS:

.comment { margin-left: 50px; }

There still remains the question of the data structure to use in Rails to represent the comments and their relationships to each other. Is there native support in Ruby for representing a tree data structure that would work well for this structure of data? Or would I need to build something customized to this task?


Solution

  • You could use one of the nested set plugins - awesome_nested_set appears to be the one most actively maintained. These allow you to select a full set (including all descendants) with a single call to the database.