The Ancestry gem works like a tree but also allows us to fetch deeply nested messages in a single query. It provides a method to arrange them after fetching.

Read more at the source