# Displaying my friends' friends in self-referential habtm

**URL:** <https://www.ruby-forum.com/t/displaying-my-friends-friends-in-self-referential-habtm/138269>\
**Category:** Rails\
**Created:** [May 21, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/displaying-my-friends-friends-in-self-referential-habtm/138269 "2008-05-21T15:45:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 21, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/displaying-my-friends-friends-in-self-referential-habtm/138269/1 "2008-05-21T15:45:12Z")

</div>

Hi guys,

I have a self-referential model of friends (based on Josh S.'s  
tutorial), i.e. basically sthg like tables ‘people’ and ‘relationships’.

What I want is to display my friends together with their friends (and  
possibly even deeper), some kind of tree

i.e. the output should look like this:

-myself  
–Bill  
—Monica  
—Hillary  
–Fidel  
—Hugo  
–Hillary  
—Bill  
—Barrack

etc.

this must be pretty easy, but I’m a newbie and failed to google it out.  
thanks

---

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 21, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/displaying-my-friends-friends-in-self-referential-habtm/138269/2 "2008-05-21T16:25:20Z")

</div>

This could help:  
[http://blog.hasmanythrough.com/2006/4/21/self-referential-through](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)

helped me, anyway.

Elise

On May 21, 3:45 pm, Pepa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 21, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/displaying-my-friends-friends-in-self-referential-habtm/138269/3 "2008-05-21T17:15:07Z")

</div>

removed\_email\_address@domain.invalid wrote:

> This could help:  
> [has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)
> 
> helped me, anyway.
> 
> Elise
> 
> On May 21, 3:45 pm, Pepa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Elisem, thank you.

actually I managed to implement the model, a variation on another Josh  
Susser’s guidance at  
[has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through).

If I use Josh’s example, I’d like to display a list of my ‘preys’  
together with their ‘preys’ as well as their other ‘predators’ and also  
my ‘predators’ as well as their ‘predators’ and their other ‘preys’.

I would be able to do that in SQL, but I’m a bit lost in the Rails  
naming conventions in this case…

Thanks for any further help
