# Acts as tree and belongs to

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-and-belongs-to/152011>\
**Category:** Rails\
**Created:** [October 28, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-belongs-to/152011 "2008-10-28T23:19:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pandabeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@pandabeer](https://www.ruby-forum.com/u/pandabeer)\
**Post date:** [October 28, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-belongs-to/152011/1 "2008-10-28T23:19:21Z")

</div>

Hello all,  
is that possible to define an association between Section and Category  
just for the root of a category?

Here is my Category model:

class Category \< ActiveRecord::Base  
acts\_as\_tree :order =\> “name”  
belongs\_to :sections # just for the root of Category  
end

Thank you.

---

<div class="post-metadata">

**Author:** ![pandabeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@pandabeer](https://www.ruby-forum.com/u/pandabeer)\
**Post date:** [October 29, 2008, 1:08am UTC](https://www.ruby-forum.com/t/acts-as-tree-and-belongs-to/152011/2 "2008-10-29T01:08:51Z")

</div>

I think you’ better write a method rather than using belongs\_to.  
In my opnion, if you use belongs\_to :xxx, than you get xxx for all the  
object of Category class, not just the root category.  
If you write a seperate method, you can write your own logic in it,  
for example, for every category, you first find its root category, and  
then return the section object of that root category.  
And I think you can just use that as the same as by using belongs\_to

On Oct 29, 7:19 am, Panda B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pandabeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@pandabeer](https://www.ruby-forum.com/u/pandabeer)\
**Post date:** [October 29, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-belongs-to/152011/3 "2008-10-29T19:58:16Z")

</div>

Understood, that’s a little bit tricky but I believe I could do it.  
Thanks for your help!
