# Many-to-many relationship to table itself through other table

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationship-to-table-itself-through-other-table/185820>\
**Category:** Rails\
**Created:** [March 26, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-to-table-itself-through-other-table/185820 "2010-03-26T17:44:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mxrgus\_pxrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mxrgus\_pxrt](https://www.ruby-forum.com/u/mxrgus_pxrt)\
**Post date:** [March 26, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-to-table-itself-through-other-table/185820/1 "2010-03-26T17:44:02Z")

</div>

Hello,

I would like to have many-to-many relationship to object itself with  
additional info, but I did not find any info on that, could someone  
suggest something?

Example:

Service  
– name  
– description

ServiceConnection  
– type  
– description  
– parent\_id  
– child\_id

---

<div class="post-metadata">

**Author:** ![mxrgus\_pxrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mxrgus\_pxrt](https://www.ruby-forum.com/u/mxrgus_pxrt)\
**Post date:** [March 26, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-to-table-itself-through-other-table/185820/2 "2010-03-26T18:00:26Z")

</div>

On 26 March 2010 11:16, mxrgus pxrt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> – name  
> – description
> 
> ServiceConnection  
> – type  
> – description  
> – parent\_id  
> – child\_id

Have a look at has\_many :through with class\_name and foreign\_key. You  
will need two of these, one for parents and one for children.  
has\_many :service\_connections  
has\_many :parents, :through =\> :service\_connection, :class\_name… etc  
has\_many :children, :through =\> :service\_connection, :class\_name…  
etc

The rails guide on activerecord associations at  
[http://guides.rubyonrails.org/](http://guides.rubyonrails.org/) should help.

Colin

---

<div class="post-metadata">

**Author:** ![mxrgus\_pxrt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@mxrgus\_pxrt](https://www.ruby-forum.com/u/mxrgus_pxrt)\
**Post date:** [March 26, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-to-table-itself-through-other-table/185820/3 "2010-03-26T20:15:34Z")

</div>

mxrgus pxrt wrote:

> Hello,
> 
> I would like to have many-to-many relationship to object itself with  
> additional info, but I did not find any info on that, could someone  
> suggest something?
> 
> Example:
> 
> Service  
> – name  
> – description
> 
> ServiceConnection  
> – type  
> – description  
> – parent\_id  
> – child\_id

* * *

Refer link given below…

> **[A Simple Twitter App with Ruby on Rails – Building Friendships - noupe](https://www.noupe.com/development/a-simple-twitter-app-with-ruby-on-rails-building-friendships.html)**
>
> This is the third and final part of series on how to create a twitter style web application with Ruby on Rails. This part will cover how to add friendships between users.
