# Belongs\_to herself

**URL:** <https://www.ruby-forum.com/t/belongs-to-herself/124451>\
**Category:** Rails\
**Created:** [December 28, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451 "2007-12-28T20:18:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 28, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/1 "2007-12-28T20:18:17Z")

</div>

Hi!  
I have a problem with belongs\_to and has\_many, the thing is  
i have a category table which points to herself if it’s a subcategory  
of another category.

I just started using ruby on rails, and don’t know how to implement  
that or if it can be done (I assume it can).

Thank for any help.

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 28, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/2 "2007-12-28T20:51:08Z")

</div>

On Dec 28, 2:17 pm, Adam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi!  
> I have a problem with belongs\_to and has\_many, the thing is  
> i have a category table which points to herself if it’s a subcategory  
> of another category.
> 
> I just started using ruby on rails, and don’t know how to implement  
> that or if it can be done (I assume it can).

You assume correctly. Here’s an example:

class Category \< ActiveRecord::base  
belongs\_to :parent\_category, :class\_name =\> ‘Category’, :foreign\_key  
=\> ‘parent\_category\_id’  
has\_many :subcategories, :class\_name =\> ‘Category’, :foreign\_key =\>  
‘parent\_category\_id’  
end

Your migration might contain this (among other stuff):

create\_table :categories do |t|  
t.column :parent\_category, :integer  
end

And now you can theoretically do things like:

subs = Category.find(1).subcategories  
parent = Category.find(2).parent

-Bill

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 28, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/3 "2007-12-28T20:52:53Z")

</div>

On Dec 28, 2:49 pm, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Your migration might contain this (among other stuff):
> 
> create\_table :categories do |t|  
> t.column :parent\_category, :integer  
> end

Woops - that, of course, should have been:

t.column :parent\_category\_id, :integer

My bad.

-Bill

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 28, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/4 "2007-12-28T21:10:29Z")

</div>

Great!!  
Thanks for a quick answer!

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 28, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/5 "2007-12-28T20:56:25Z")

</div>

On Dec 28, 2:49 pm, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And now you can theoretically do things like:
> 
> subs = Category.find(1).subcategories  
> parent = Category.find(2).parent

And this should have been:

parent = Category.find(2).parent\_category

Sorry for the extra messages. I wasn’t being as careful and thorough  
as I should have been.

-Bill

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 29, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/6 "2007-12-29T12:50:51Z")

</div>

OK, thanks!!  
I’ll look that up too.

On 29 Gru, 06:44, “Jeffrey L. Taylor” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 29, 2007, 6:44am UTC](https://www.ruby-forum.com/t/belongs-to-herself/124451/7 "2007-12-29T06:44:50Z")

</div>

Quoting A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi!  
> I have a problem with belongs\_to and has\_many, the thing is  
> i have a category table which points to herself if it’s a subcategory  
> of another category.
> 
> I just started using ruby on rails, and don’t know how to implement  
> that or if it can be done (I assume it can).

Look at acts\_as\_tree. It’s a plugin in Rails 2.0, builtin in Rails 1.2.

Example:

class Task \< ActiveRecord::Base  
has\_many :comments, :dependent=\>:destroy  
end

class Comment \< ActiveRecord::Base  
belongs\_to :task  
acts\_as\_tree :order=\>‘created\_at’  
end

Tasks have a hierarchy of comments with replies and replies can have  
replies  
arbitrarily deep.

task = Task.find(id)  
task.comments # top level comments  
task.comments[0].comments # replies to first top level comment

comment is like a category, reply is like a sub-category.

HTH,  
Jeffrey
