# Many to Many Relationship with One Model

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationship-with-one-model/147180>\
**Category:** Rails\
**Created:** [August 29, 2008, 4:48am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-with-one-model/147180 "2008-08-29T04:48:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![starnix17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@starnix17](https://www.ruby-forum.com/u/starnix17)\
**Post date:** [August 29, 2008, 4:48am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-with-one-model/147180/1 "2008-08-29T04:48:09Z")

</div>

Hey everyone,

I’m working on allow users to follow other users in my app so they can  
receive updates to what all their friends are doing (like Facebook/  
Twitter). I’m wondering on how the relationship would be for the  
model. Sounds like a HABTM type of association, but how would I go  
about doing it for only one model?

Thanks,  
Tony

---

<div class="post-metadata">

**Author:** ![starnix17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@starnix17](https://www.ruby-forum.com/u/starnix17)\
**Post date:** [August 29, 2008, 6:24am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-with-one-model/147180/2 "2008-08-29T06:24:04Z")

</div>

I’ll start w/ the User class.

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :friends,  
:class\_name =\> “User”, # [1]  
:join\_table =\> “users\_friends”, # [2]  
:foreign\_key =\> “user\_id”, # [3]  
:association\_foreign\_key =\> “friend\_id” # [4]  
end

[1] Specify the class name since it differs from the association name.  
[2] Specify the join table since you’ll name it something other than  
users\_users, which is what Rails would assume.  
[3] Specify the foreign key back to this user; not necessary as Rails  
will  
assume it in this case, but I thought I’d show it.  
[4] Specify the foreign key that will hold the ids of this user’s  
friends.

Here’s the migration to create the join table.

class … \< ActiveRecord::Migration  
def self.up  
create\_table :users\_friends, :id =\> false, :force =\> true do |t|  
t.integer :user\_id,  
t.integer :friend\_id  
end  
end

def self.down  
drop\_table :users\_friends  
end  
end

As you can see, it’s just a simple join table for tracking the  
relationship  
from user to user (friend).

I’m sure you’ll want to tweak the User class and migration a bit, but I  
hope  
you get the idea.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![starnix17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@starnix17](https://www.ruby-forum.com/u/starnix17)\
**Post date:** [August 29, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-with-one-model/147180/3 "2008-08-29T20:52:55Z")

</div>

That’s exactly was I was trying to pull off, perfect explanation!

Thanks a lot Craig!

On Aug 29, 12:23 am, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
