# Self-referencing model

**URL:** <https://www.ruby-forum.com/t/self-referencing-model/150397>\
**Category:** Rails\
**Created:** [October 8, 2008, 2:53am UTC](https://www.ruby-forum.com/t/self-referencing-model/150397 "2008-10-08T02:53:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [October 8, 2008, 2:53am UTC](https://www.ruby-forum.com/t/self-referencing-model/150397/1 "2008-10-08T02:53:41Z")

</div>

I’m fairly new to Ruby on Rails, and am having trouble figuring out how  
to do this. I have a model called Organization. I want to set things  
up so that an org can have sub-orgs (which can have sub-orgs  
themselves).

Can anyone point me to a good example or explanation (or provide one  
directly) so I can get an idea of how to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [October 8, 2008, 3:11am UTC](https://www.ruby-forum.com/t/self-referencing-model/150397/2 "2008-10-08T03:11:18Z")

</div>

I’ve just been doing this myself with systems and sub-systems.

In the system model I have:

has\_many :sub\_systems, :class\_name =\> “System”, :foreign\_key =\>  
“system\_id”, :dependent=\>:nullify

Then you can refer to the system.sub\_systems

I also have code to prevent loops which will go around forever if you  
traverse the system-sub-system tree.

def validate  
errors.add(:sub\_systems, “Cannot be a subsystem of itself.”) if  
all\_subs.include?(self)  
end

def all\_subs(subs = [])  
self.sub\_systems.each do |s|  
unless subs.include?(s)  
subs \<\< s  
s.all\_subs(subs)  
end  
end  
subs  
end

Hope that helps.

On Oct 8, 10:53 am, David H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [October 8, 2008, 3:14am UTC](https://www.ruby-forum.com/t/self-referencing-model/150397/3 "2008-10-08T03:14:50Z")

</div>

I forgot this - you also have to have

belongs\_to :system

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [October 8, 2008, 5:52am UTC](https://www.ruby-forum.com/t/self-referencing-model/150397/4 "2008-10-08T05:52:56Z")

</div>

Acts as Tree:

[http://wiki.rubyonrails.org/rails/pages/ActsAsTree](http://wiki.rubyonrails.org/rails/pages/ActsAsTree)

Does everything you need.

On Oct 7, 8:53 pm, David H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [October 8, 2008, 3:11am UTC](https://www.ruby-forum.com/t/self-referencing-model/150397/5 "2008-10-08T03:11:19Z")

</div>

Look into the :as and :source options for the has\_many and belongs\_to  
associations, they should be able to do what you need.

Good luck!

On Oct 7, 5:53 pm, David H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
