Reputation: 533
My comment structure works like this (I'm using acts_as_commentable_with_threading).
Comment 1 (id: 1) has parent_id of nil because it is not a reply to any comment. Comment 2 (id: 2) has a parent_id of 1 because it is a reply to comment 1 which has id of 1. Comment 3 (id: 3) has a parent_id of 2 because it is a reply to comment 2. Comment 4 (id: 4) has a parent_id of 1 because it is a reply to comment 1.
What is the best way of loading all the replies for a parent comment (in this case Comment 1). I could use multiple loops but I assume that would be really inefficient. For each reply, I would have to generate a partial to show that reply.
I would need a function that returns all of the children of a parent comment and the children of it's children, etc.
Upvotes: 0
Views: 79
Reputation: 106952
Loading nested sets by multitple database queries is inefficient. There are better ways to organize nested data. Have a look at how you could implement a nested set in SQL or use a gem like awesome_nested_set that allows to load all nodes with one query.
Upvotes: 1