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.