# Question about acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/question-about-acts-as-tree/122242>\
**Category:** Rails\
**Created:** [December 5, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242 "2007-12-05T21:19:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/1 "2007-12-05T21:19:35Z")

</div>

Hi All,

I was wondering if a Class declared with acts\_as\_tree can have more  
than one root, i.e., parent\_id = null?

this will basically support multiple trees within the same table.

My implementation idea is to have a lookups table where the records  
that have parent\_id are the lookup Types and each one of those will  
have several children - and children could have more children, etc.

I know I can achieve the same thing for creating a dummy root and make  
the first level my “types”, but I thought the idea above is cleaner in  
my opinion.

thanks,

Marcelo.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 3:34am UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/2 "2007-12-06T03:34:47Z")

</div>

I’ve been tackling acts\_as\_tree in trying to write forum software. The  
original posts are each a root, with the replies as children. So, yes,  
you can have as many roots as you want.

The challenge I am faced with is how to traverse a full tree to get  
the last post???

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 3:38am UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/3 "2007-12-06T03:38:19Z")

</div>

Types… you mean like “topics”, right?

I feel that going with a forums + topics + posts database schema is a  
much  
better idea than acts\_as\_tree, but that’s just me.

On Dec 6, 2007 6:49 AM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> have several children - and children could have more children, etc.
> 
> I know I can achieve the same thing for creating a dummy root and make  
> the first level my “types”, but I thought the idea above is cleaner in  
> my opinion.
> 
> thanks,
> 
> Marcelo.
> 
> >

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 3:44am UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/4 "2007-12-06T03:44:46Z")

</div>

Ryan–Can you explain the “I feel that going with a forums + topics +  
posts database schema is a much better idea than acts\_as\_tree” a  
little more?

Are you suggesting that ‘topic’ be segregated into it’s own table? Now  
I have now a forums + (posts w/topics) schema.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 4:11am UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/5 "2007-12-06T04:11:05Z")

</div>

> Are you suggesting that ‘topic’ be segregated into it’s own table? Now I  
> have now a forums + (posts w/topics) schema.

Indeed I am suggesting this.

A forum has many topics, topics have many posts and then posts belong to  
topics. I find it easier to delete all posts from a topic this way  
(instead  
of iterating through all the posts and then deleting all of their  
children,  
it’s basically a DELETE FROM `posts` WHERE `topic_id` = ‘topic.id’). I  
also  
allow for topics to have their own subjects. I’ve never found a use for  
a  
tree view in a forum system. If anyone has anything to say about another  
post I find that they [quote] the relevant sections of the first post  
and  
then reply to it.

On Dec 6, 2007 1:14 PM, yachtman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 6, 10:37 am, “Ryan B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > have several children - and children could have more children, etc.  
> > > Ryan B.

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 4:50am UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/6 "2007-12-06T04:50:19Z")

</div>

For some reason when I posted the message before yours I had a feeling  
like  
it was going to get shot down in flames (for a reason that still evades  
me,  
I guess the Internet’s conditioned me).

Instead, it got praised. Thanks 🙂 I was “inspired” by vBulletin and  
phpbb.

On Dec 6, 2007 1:58 PM, yachtman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> a
> 
> > > > I feel that going with a forums + topics + posts database schema is  
> > > > more
> > 
> > > > –  
> > > > Ryan B.
> > 
> > –  
> > Ryan B.

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/7 "2007-12-06T15:23:10Z")

</div>

Oh great,

So quick question: (maybe two!)  
1 - What happens when you call the root() method?  
2 - how do you get a specific root()?  
3 - How do you get all the roots?

thanks,

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 4:29am UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/8 "2007-12-06T04:29:13Z")

</div>

Ryan, Thanks for the help. I’ve been designing schemas for years and I  
don’t know why that layout didn’t occur to me. Yikes. It’s so darn  
simple. I’ve already got it implemented and it works great.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/question-about-acts-as-tree/122242/9 "2007-12-06T15:31:23Z")

</div>

To get all of the roots…parent\_id will be null (this is a requirement  
of acts\_as\_tree)  
parent\_posts = Post.find(:all, :conditions =\> “parent\_id is NULL”

A specific root, you’ll need the id…  
post = Post.find\_by\_id(id)

Not sure what calling the root method means…
