Nested Comments in Ruby on Rails

Lucy Suddenly on March 10, 2019

Nested comments was a highly-desirable deliverable for my most recent project, so I learned how to make it happen! Here are the steps necessary... [Read Full]
markdown guide

Nice post.

DEV is a Rails app that has nested comments. Our code looks a lot like this. We use Ancestry to help manage this as well.

GitHub logo stefankroes / ancestry

Organise ActiveRecord model into a tree structure

Build Status Coverage Status Gitter Security


Ancestry is a gem that allows the records of a Ruby on Rails ActiveRecord model to be organised as a tree structure (or hierarchy). It uses a single database column, using the materialised path pattern. It exposes all the standard tree structure relations (ancestors, parent, root, children, siblings, descendants) and all of them can be fetched in a single SQL query. Additional features are STI support, scopes, depth caching, depth constraints, easy migration from older gems, integrity checking, integrity restoration, arrangement of (sub)tree into hashes and different strategies for dealing with orphaned records.


  • Ancestry 2.x supports Rails 4.1, and earlier
  • Ancestry 3.x supports Rails 5.0, and 4.2
  • Ancestry 4.0 only supports rails 5.0 and higher


To apply Ancestry to any ActiveRecord model, follow these simple steps:


  • Add to Gemfile:
# Gemfile
gem 'ancestry'
  • Install required gems:
$ bundle install

Add ancestry column to your


Wow! If I had known about this shortcut I would have taken it. Thanks for sharing!


Thanks for the post, I'm learning rails and trying to implement nested comments on a project of mine, the problem is that I get an error that comments is an undefined method on my comment controller create. Is there any hidden steps that I should've done in between?


I'm implement comments the same way to you but now I want use Action Cable and I started learn about this resource.

code of conduct - report abuse