# Need help with self-referencing models and relationships

**URL:** <https://www.ruby-forum.com/t/need-help-with-self-referencing-models-and-relationships/128071>\
**Category:** Rails\
**Created:** [February 4, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/need-help-with-self-referencing-models-and-relationships/128071 "2008-02-04T20:15:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [February 4, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/need-help-with-self-referencing-models-and-relationships/128071/1 "2008-02-04T20:15:57Z")

</div>

Okay,

I have the site running ([http://www.londonprogressivejournal.com](http://www.londonprogressivejournal.com)) and if  
you look you can ‘respond’ to any given article which leads to you  
submitting a ‘letter’ object to the system.

At a later date we’ll be publishing these.

Articles can of course have many letters. Letters only have one article.

However, people will be able to respond to a published letter just as  
they will to an article.

So a Letter can have many letters (those that are responses to it)…but  
it also only has one letter if it is itself a response to an existing  
letter.

My Letter model has both an article\_id and a letter\_id but my issue is  
how to show Ruby that model? I have said ‘has many letters’ but now of  
course I can’t refer to letter.letter.id to find out which letter this  
one might be a response to.

Does anyone know how to deal with this sort of self-referential  
architecture in ruby?

Cheers  
Theo

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [February 5, 2008, 12:37am UTC](https://www.ruby-forum.com/t/need-help-with-self-referencing-models-and-relationships/128071/2 "2008-02-05T00:37:41Z")

</div>

Theo Graham-brown wrote:

> Okay,
> 
> I have the site running ([http://www.londonprogressivejournal.com](http://www.londonprogressivejournal.com)) and if  
> you look you can ‘respond’ to any given article which leads to you  
> submitting a ‘letter’ object to the system.
> 
> At a later date we’ll be publishing these.
> 
> Articles can of course have many letters. Letters only have one article.
> 
> However, people will be able to respond to a published letter just as  
> they will to an article.
> 
> So a Letter can have many letters (those that are responses to it)…but  
> it also only has one letter if it is itself a response to an existing  
> letter.
> 
> My Letter model has both an article\_id and a letter\_id but my issue is  
> how to show Ruby that model? I have said ‘has many letters’ but now of  
> course I can’t refer to letter.letter.id to find out which letter this  
> one might be a response to.
> 
> Does anyone know how to deal with this sort of self-referential  
> architecture in ruby?
> 
> Cheers  
> Theo

Let me restate your problem from the Letter’s point of view. A Letter  
belongs\_to a parent (either a Article or another Letter). It also  
has\_many responses (always other letters). I would build you letter  
class like this.

class Letter \< ActiveRecord::Base  
belongs\_to :parent, :polymorphic=\>true  
has\_many :responses, :class\_name =\>‘Letter’, :as=\>:parent  
end

Same thing for Article

class Article \< ActiveRecord::Base  
has\_many :letters, :as=\>:parent  
end

As a side benefit, you can attach a letter to anything else on your  
site. You may also want to add validation on the belongs\_to association  
to ensure that it only contains the classes you wish to attach letters  
to.

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [February 4, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/need-help-with-self-referencing-models-and-relationships/128071/3 "2008-02-04T21:01:28Z")

</div>

You can define these relationships yourself using something like this:  
class Letter \< ActiveRecord::Base  
belongs\_to :article  
belongs\_to :parent, :foreign\_key =\> ‘parent\_id’, :class\_name =\>  
‘Letter’  
has\_many :children, :foreign\_key =\> 'parent\_id, :class\_name =\>  
‘Letter’, :dependent =\> :destroy  
end

But I would recommend taking a look at the acts\_as\_tree and  
acts\_as\_nested\_set plugins and see if they might provide what you are  
looking for. acts\_as\_tree provides the parent and children  
associations like I listed above. acts\_as\_nested set allows you to  
retrieve all children in a single query. ([http://dev.rubyonrails.com/](http://dev.rubyonrails.com/)  
svn/rails/plugins/)

Aaron

On Feb 4, 12:15 pm, Theo Graham-brown \<rails-mailing-l…@andreas-
