Few days back, I was working on a project where I have to create nested comments on a articles and on showing the article I was needed to show all the nested comments in its hierarchy.So I used closure_tree gem to achieve that. So lets see how I implemented the nested comment functionality on article.
1) Add the following Gem to your Gemfile.
2) Run the bundle to install the Gem
3) Now create the comments model which will have article_id (in case of root comment which will be posted directly on the article), parent_id (for storing the parent comment id), user_id, text (to store the content of the text)
rails g model Comment article:references parent_id:integer text:string
4) Now run the following migration for getting nested comments hierarchy
rails g migration create_comment_hierarchies
5) Inside this migration write the following code
class CreateCommentHierarchies < ActiveRecord::Migration
create_table :comment_hierarchies, :id => false do |t|
t.integer :ancestor_id, :null => false # ID of the parent/grandparent/great-grandparent/... comments
t.integer :descendant_id, :null => false # ID of the target comment
t.integer :generations, :null => false # Number of generations between the ancestor and the descendant. Parent/child = 1, for example.
# For "all progeny of" and leaf selects:
add_index :comment_hierarchies, [:ancestor_id, :descendant_id, :generations],
:unique => true, :name => "comment_anc_desc_udx"
# For "all ancestors of" selects,
add_index :comment_hierarchies, [:descendant_id],
:name => "comment_desc_idx"