# Self referential model?

**URL:** <https://www.ruby-forum.com/t/self-referential-model/177941>\
**Category:** Rails\
**Created:** [October 27, 2009, 1:02am UTC](https://www.ruby-forum.com/t/self-referential-model/177941 "2009-10-27T01:02:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [October 27, 2009, 1:02am UTC](https://www.ruby-forum.com/t/self-referential-model/177941/1 "2009-10-27T01:02:14Z")

</div>

is it possible to have a model be belong to itself?

say i have a category, and the category can be a subcategory of another  
category (or it might be a parent category)…

is this possible, and does it make sense?

something like:

class Category \< ActiveRecord::Base  
has\_many: categories  
end

i kinda think it would make more sense to use a subcategory model… but  
the system i am deriving from (which was written in php) is combining  
the models…

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [October 27, 2009, 1:14am UTC](https://www.ruby-forum.com/t/self-referential-model/177941/2 "2009-10-27T01:14:19Z")

</div>

Robert W. wrote:

> Sergio R. wrote:
> 
> > is it possible to have a model be belong to itself?

There is also the [GitHub - rails/acts\_as\_tree: NOTICE: official repository moved to https://github.com/amerine/acts\_as\_tree](http://github.com/rails/acts_as_tree/) plugin that  
implements a simple tree structure and may be adequate for your needs.

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [October 27, 2009, 1:10am UTC](https://www.ruby-forum.com/t/self-referential-model/177941/3 "2009-10-27T01:10:10Z")

</div>

Sergio R. wrote:

> is it possible to have a model be belong to itself?
> 
> say i have a category, and the category can be a subcategory of another  
> category (or it might be a parent category)…

> **[GitHub - collectiveidea/awesome\_nested\_set: An awesome replacement for...](https://github.com/collectiveidea/awesome_nested_set/)**
>
> An awesome replacement for acts\_as\_nested\_set and better\_nested\_set. - GitHub - collectiveidea/awesome\_nested\_set: An awesome replacement for acts\_as\_nested\_set and better\_nested\_set.

> is this possible, and does it make sense?

Yes, it makes sense.

> something like:
> 
> class Category \< ActiveRecord::Base  
> has\_many: categories  
> end
> 
> i kinda think it would make more sense to use a subcategory model… but  
> the system i am deriving from (which was written in php) is combining  
> the models…

No. As you’ll see from the above plugin using one model does make more  
sense.

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [October 27, 2009, 3:56am UTC](https://www.ruby-forum.com/t/self-referential-model/177941/4 "2009-10-27T03:56:54Z")

</div>

thanks, guys… installed awesome\_nested\_set now… playing with it…  
looks cool…

i am guessing that there is no drop in method of using this with  
active\_scaffold… correct?

anyway…

this looks great!

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [October 27, 2009, 9:29am UTC](https://www.ruby-forum.com/t/self-referential-model/177941/5 "2009-10-27T09:29:19Z")

</div>

you can read rails guides.

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html#polymorphic-associations)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

class Employee \< ActiveRecord::Base  
has\_many :subordinates, :class\_name =\> “Employee”, :foreign\_key =\>  
“manager\_id”  
belongs\_to :manager, :class\_name =\> “Employee”  
end

With this setup, you can retrieve @employee.subordinates and  
@employee.manager.

2009/10/27 Sergio R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> class Category \< ActiveRecord::Base
> 
> >

–  
tommy xiao  
E-mail: [xiaodsATgmail.com](http://xiaodsATgmail.com)

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [October 27, 2009, 5:33am UTC](https://www.ruby-forum.com/t/self-referential-model/177941/6 "2009-10-27T05:33:23Z")

</div>

Sergio R. wrote:

> thanks, guys… installed awesome\_nested\_set now… playing with it…  
> looks cool…

awesome\_nested\_set is great. If you’re not already familiar with nested  
sets, make sure to read Joe Celko’s and Vadim Tropashko’s introductory  
articles.

> i am guessing that there is no drop in method of using this with  
> active\_scaffold… correct?

Probably. But parent\_id shouldn’t present much trouble with  
ActiveScaffold.

> anyway…
> 
> this looks great!
> 
> thanks!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
