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

  • Home
  • SEARCH
  • 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 7028511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:23:50+00:00 2026-05-28T00:23:50+00:00

In my ruby-on-rails app, I have nested comments that can be nested an arbitrary

  • 0

In my ruby-on-rails app, I have nested comments that can be nested an arbitrary length.

I tried different ways of storing this:

Using self joins:

belongs_to :parent, :class_name => 'Comment', :foreign_key => 'parent_id'
has_many :children, :class_name => 'Comment', :foreign_key => "parent_id"

Using ancestry gem

etc

The problem, though, is that no matter what I use, there will always be an linear number of SQL statements. (1 statement to grab all the root comments, and then 1 statement for each root’s children, and then 1 statement for all the children of that, etc)

Is there a more efficient way to accomplish this?

Postgres 9.1, but hopefully backwards compatible solutions are preferred.

  • 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-28T00:23:51+00:00Added an answer on May 28, 2026 at 12:23 am

    You could stick with your parent_id pointer column and use find_by_sql and a WITH RECURSIVE query and let the database do all the work in one shot. Something like this:

    comments = Comment.find_by_sql(%Q{
        with recursive tree(id) as (
            select c.id, c.column1, ...
            from comments c
            where c.id in (#{roots.join(',')})
            union all
            select c.id, c.column1, ...
            from comments c
            join tree on c.parent_id = tree.id
        )
        select id, column1, ...
        from tree
    })
    

    where roots would be a Ruby array holding the ids of the root nodes that you’re interested in. That will give you all the nodes in the subtrees of interest as Comment instances. I’ve used queries like this in the past and WITH RECURSIVE was well over twice as fast as your iterative technique even with shallow trees, I’d guess that deeper trees would see even better speed ups.

    The parent_id structure you’re using is very convenient for most things and meshes quite well with how ActiveRecord wants to work. Also, sticking with your current structure means that you can leave the rest of your application alone.

    WITH RECURSIVE is available in PostgreSQL 8.4 and higher.

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

Sidebar

Related Questions

I have a ruby on rails app that has a signup page. Different pages
I have a Ruby on Rails app running on my server, and I can't
I have a Ruby on Rails app that I've recently deployed to a remote
I have a Ruby script in my Rails app that I use to load
I have a Ruby on Rails app that needs process many background jobs simultaneously:
Some of the users of our Ruby on Rails app have complained that page
I have a Ruby on Rails app that has a scheduled job I execute
I have a Ruby on Rails app that I'm working on and I'm having
I have a Ruby on Rails app that I built myself, but which needs
I have built a ruby on rails app that lets users track their workouts.

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.