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?
acts_as_nested_set
That allows you to get all children in one query.
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…
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