re: Nested Comments in Ruby on Rails VIEW POST


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!

code of conduct - report abuse