# Forum in Rail--Acts\_as\_Tree?

**URL:** <https://www.ruby-forum.com/t/forum-in-rail-acts-as-tree/122169>\
**Category:** Rails\
**Created:** [December 5, 2007, 4:29am UTC](https://www.ruby-forum.com/t/forum-in-rail-acts-as-tree/122169 "2007-12-05T04:29:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [December 5, 2007, 4:29am UTC](https://www.ruby-forum.com/t/forum-in-rail-acts-as-tree/122169/1 "2007-12-05T04:29:43Z")

</div>

Working on a forum in rails, but I am stumped on the storing/  
retrieving of forum posts.

Using Acts\_as\_Tree, but can’t figure out how to query my model to get  
a list of the most recent posts in every thread.

Model Post  
id  
parent\_id  
post  
created\_at

This gets the parent of each thread…  
parent\_posts = Post.find(:all, :conditions =\> “parent\_id is NULL”)

This gets all of the posts in a thread that are siblings…  
sibling\_posts = Post.children.find(parent\_post)

Any ideas please?

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [December 5, 2007, 5:53am UTC](https://www.ruby-forum.com/t/forum-in-rail-acts-as-tree/122169/2 "2007-12-05T05:53:53Z")

</div>

acts\_as\_nested\_set

That allows you to get all children in one query.

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [December 5, 2007, 6:18am UTC](https://www.ruby-forum.com/t/forum-in-rail-acts-as-tree/122169/3 "2007-12-05T06:18:26Z")

</div>

s.ross - Seems like this is overly-complicated since it utilizes \>  
root \> children \> children’s children etc…

When in my case, it’s either:  
#1: root \> child \> child’s child \> child’s child’s child (Each  
initial post is a root, and each reply (child) is a reply to the last  
child  
or  
#2: root \> children (Each initial post is the root, and the replies  
with be the timestamped children)

If I go with #2, then at most I have two layers - Roots (Each initial  
post in the forum) and many direct children of each root (the  
timestamped–which will give me the order–replies).

The overhead seems high with acts\_as\_nested\_set…

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [December 5, 2007, 8:03am UTC](https://www.ruby-forum.com/t/forum-in-rail-acts-as-tree/122169/4 "2007-12-05T08:03:39Z")

</div>

If you were modeling this (your scenario 1), you might use a linked  
list or doubly linked list. Right? Would acts\_as\_list work? That  
would preserve order and of course the scope of the list would be the  
thread id or something sensible like that.

Your scenario 2 is something of a bag. No real positional  
relationship implied, you handle the ordering based on the data.

Perhaps acts\_as\_tree is not what you were looking for. The only  
reason I suggested aans is because if has proven effective for me at  
partitioning threaded topical discussions where there is not a strict  
linear descendency. For example:

- (Poster 1) when is rails 2.0 gonna be released
  - (Poster 2) re: when is rails 2.0 gonna be released
    - (Poster 1) re: when is rails 2.0 gonna be released

  - (Poster 3) re: when is rails 2.0 gonna be released

This is more the slashdot model and requires a bit more tracking of  
who replied to whom within a thread.

-s
