# Self Referential has\_many :through broken functionality?

**URL:** <https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683>\
**Category:** Rails\
**Created:** [March 22, 2007, 1:21am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683 "2007-03-22T01:21:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pathologic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pathologic](https://www.ruby-forum.com/u/pathologic)\
**Post date:** [March 22, 2007, 1:21am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683/1 "2007-03-22T01:21:49Z")

</div>

Having set up my self referential user -\> friend relationship with the  
following code all seems to be working well except for one thing. Adding  
and deleting friends. Here is the code for the models:  
class Friendship \< ActiveRecord::Base

belongs\_to :friendshipped,  
:foreign\_key =\> “user\_id”,  
:class\_name =\> “User”  
belongs\_to :befriendshipped,  
:foreign\_key =\> “friend\_id”,  
:class\_name =\> “User”

end

class User \< ActiveRecord::Base

has\_many :friendships,  
:foreign\_key =\> ‘user\_id’,  
:class\_name =\> ‘Friendship’  
has\_many :befriendships,  
:foreign\_key =\> ‘friend\_id’,  
:class\_name =\> ‘Friendship’

has\_many :friends,  
:through =\> :friendships,  
:source =\> :befriendshipped

has\_many :befrienders,  
:through =\> :befriendships,  
:source =\> :friendshipped

end

When i execute the following code strange things happen:

user = User.find 1

friend = User.find 2

user.friends \<\< friend

This piece of code appears to work but upon inspection of the generated  
sql a friendship record is created with user\_id = 2 where it should be 1  
and the friend\_id is set to NULL where it should be 2. It seems that  
the ability to add friends to a user this way using self-referential  
relationships is broken. Also deleting does not working either if i go  
user.friends.delete(friend) an update statement is generated, very  
confusing. I am not sure whats going on here at all. This functionality  
works for normal has\_many :through relationships just not my self  
referential ones. Any ideas?

---

<div class="post-metadata">

**Author:** ![pathologic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pathologic](https://www.ruby-forum.com/u/pathologic)\
**Post date:** [March 22, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683/2 "2007-03-22T23:10:42Z")

</div>

For anyone that may stumble upon this i have found a workaround although  
i still believe it is a bug in rails.

When your self referential join is setup like above to add friends to a  
user instead of using this :

```
  user = User.find 1

  friend = User.find 2

  user.friends << friend

```

Use this:

```
  user = User.find 1

  friend = User.find 2

  user.friendships.create(:user_id => user.id, :friend_id => 

```

friend.id)

For deleting instead of using this:

```
  user.friends.delete(friend)

```

Use this:

```
 user.friendships.destroy(user.friendships.find_by_friend_id(friend))

```

Hopefully this helps someone, any input from someone more experienced  
than me is more than welcome, as i claim to be no expert.

---

<div class="post-metadata">

**Author:** ![pathologic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pathologic](https://www.ruby-forum.com/u/pathologic)\
**Post date:** [March 24, 2007, 3:26am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683/3 "2007-03-24T03:26:30Z")

</div>

I ave noticed this behaviour also. Can anyone confirm if this is a bug  
or not ?? How does one confirm this.

---

<div class="post-metadata">

**Author:** ![pathologic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pathologic](https://www.ruby-forum.com/u/pathologic)\
**Post date:** [March 24, 2007, 5:42am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683/4 "2007-03-24T05:42:21Z")

</div>

I have created a plugin called Simply Rich Association that simplifies  
the  
self referential many to many relationship implementation. Check out the  
Rubyforge for details:

[http://sra.rubyforge.org](http://sra.rubyforge.org)

I am still working on setting up the Subversion repository. For now, you  
can  
unzip the file [http://www.zepho.com/download/simply\_rich\_association.zip](http://www.zepho.com/download/simply_rich_association.zip)  
into your vendor plugin directory.

---

<div class="post-metadata">

**Author:** ![pathologic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@pathologic](https://www.ruby-forum.com/u/pathologic)\
**Post date:** [March 27, 2007, 4:09am UTC](https://www.ruby-forum.com/t/self-referential-has-many-through-broken-functionality/89683/5 "2007-03-27T04:09:03Z")

</div>

@Bala P.

This is find for habtm but what about has\_many :through for join tables  
that need extra columns.
