# Self-referential symmetrical relation problem

**URL:** <https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290>\
**Category:** Rails\
**Created:** [March 9, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290 "2007-03-09T13:41:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 9, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290/1 "2007-03-09T13:41:55Z")

</div>

I found the following code here:

> **[RailsNotes — The Ruby on Rails guides you wished you had.](https://railsnotes.xyz/)**
>
> RailsNotes 🟥 is a collection of the Ruby on Rails guides you wished you had, when you were first starting out. We have interesting Ruby on Rails articles for topics like Hotwire, Stimulus, ActionMailer, Databases, Deployment and more. Whether you're...

I 'm trying to add an additional “has\_many :teamates” on the player  
model but i cant figure out how to do it…

Any ideas?

Thanks in advance!  
Chris

The Code:

# class Player \< ActiveRecord::Base

# has\_many :team\_memberships

# has\_many :teams, :through =\> :team\_memberships

# end

# 

# class TeamMembership \< ActiveRecord::Base

# belongs\_to :player

# belongs\_to :team

# end

# 

# class Team \< ActiveRecord::Base

# has\_many :team\_memberships

# has\_many :players, :through =\> :team\_memberships

# has\_many :ratings

# end

# 

# class Rating \< ActiveRecord::Base

# belongs\_to :team

# end

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 9, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290/2 "2007-03-09T15:41:28Z")

</div>

class Player \< ActiveRecord::Base  
has\_many :teammates, :class\_name =\> ‘Player’  
end

Then just add the field player\_id to the players table, and it should  
work  
fine.

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 9, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290/3 "2007-03-09T16:23:38Z")

</div>

Ï/Ç Luke I. Ýãñáøå:

> class Player \< ActiveRecord::Base  
> has\_many :teammates, :class\_name =\> ‘Player’  
> end
> 
> Then just add the field player\_id to the players table, and it should work  
> fine.

Nope, that wouldn’t work. What i want is to find the teammates through  
the other tables.

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 12, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290/4 "2007-03-12T15:00:43Z")

</div>

On Mar 9, 6:26 pm, “eden li” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> … i _think_ that works…

Thanks a lot eden li! 🙂

---

<div class="post-metadata">

**Author:** ![Trochalakis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@Trochalakis\_C](https://www.ruby-forum.com/u/Trochalakis_C)\
**Post date:** [March 9, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/self-referential-symmetrical-relation-problem/88290/5 "2007-03-09T17:35:00Z")

</div>

You’ll likely need to go to SQL here… Join the tables together and  
return the result based on the memberships of the instance-level  
player.

has\_and\_belongs\_to\_many :teammates,  
:join\_table =\> TeamMembership.table\_name,  
:finder\_sql =\> “SELECT p2.\* FROM #{TeamMembership.table\_name} AS  
t1 INNER JOIN #{Player.table\_name} AS p1 ON t1.player\_id = p1.id INNER  
JOIN #{Player.table\_name} AS p2 ON p2.team\_id = t1.team\_id WHERE p1.id  
= #{id} AND p2.id != #{id}”

… i _think_ that works…

On Mar 9, 7:49 pm, “Trochalakis C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
