Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3459378
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:05:57+00:00 2026-05-18T10:05:57+00:00

I need to build a nested comments system in a Rails 3 application that

  • 0

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. I need to be able to add comments to multiple models
  2. I need to be able to add comments to comments, infinitely deep
  3. I need to be able to efficiently retrieve all descendants for a post, article, etc
  4. I need to be able to efficiently present the comments in their appropriate nesting

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:05:57+00:00Added an answer on May 18, 2026 at 10:05 am

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

    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 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.

    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.

    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>
    

    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.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a push system in django, basicly its function is to
I need to build a configuration panel in the Plone control panel that store
I need to build a status dashboard that reads Events (ID, Desc, Time) out
I need some help concerning nested attributes in models with 'has_one' relationship. Model Survey
I need to build a url inside a controller for a nested resource: http://0.0.0.0:3000/account/1/address/new
I need to build a project consisting of many C source and header files.
So, I need some input refactoring an asp.net (c#) application that is basically a
I have a single table and I need to build a bunch of nested
I have a set of nested Ant build files, and I need to control
This I'm trying for transfer my current Apache/Modperl site to Starman, and need build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.