# Need some info regarding "how to add friends"

**URL:** <https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998>\
**Category:** Rails\
**Created:** [June 28, 2007, 8:07am UTC](https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998 "2007-06-28T08:07:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@gow](https://www.ruby-forum.com/u/gow)\
**Post date:** [June 28, 2007, 8:07am UTC](https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998/1 "2007-06-28T08:07:34Z")

</div>

hi every one,

i’m new to ruby on rails.so, need some help.

my project is that a user can add friends to his profile where friends  
are users themselves.

i thought of creating two tables one for users and other for  
user\_friends.i don’t know how to relate these two tables so that they  
serve my purpose.

any suggestions plz.

thanks in advance.

---

<div class="post-metadata">

**Author:** ![gow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@gow](https://www.ruby-forum.com/u/gow)\
**Post date:** [June 28, 2007, 8:50am UTC](https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998/2 "2007-06-28T08:50:42Z")

</div>

I am also new but I think you just need to add to Users model:  
has\_and\_belongs\_to\_many :users.

---

<div class="post-metadata">

**Author:** ![gow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@gow](https://www.ruby-forum.com/u/gow)\
**Post date:** [June 28, 2007, 9:51am UTC](https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998/3 "2007-06-28T09:51:29Z")

</div>

check out self referential associations - it is exactly what you are  
looking for.  
the problem with doing has\_and\_belongs\_to\_many :users, is that you will  
still need a join table that ‘holds’ these associated records.

give it time, it looks daunting at first, but it works. 🙂  
a couple links:

i worked with this

- [has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)  
although it is clearly outdated, and there are probably better things  
out there.  
i also googled for a sec, and found
- 

[http://lists.rubyonrails.org/pipermail/rails/2006-February/021193.html](http://lists.rubyonrails.org/pipermail/rails/2006-February/021193.html)

- [http://blog.dnite.org/2007/6/8/howto-has\_many\_friends](http://blog.dnite.org/2007/6/8/howto-has_many_friends)  
(same plugin)
- [http://blog.dnite.org/2007/3/14/has\_many\_friends-released](http://blog.dnite.org/2007/3/14/has_many_friends-released)

anyway … check out google for self\_referential; that’s the way to go.  
hope this helps!

shai

---

<div class="post-metadata">

**Author:** ![gow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@gow](https://www.ruby-forum.com/u/gow)\
**Post date:** [June 29, 2007, 6:04am UTC](https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998/4 "2007-06-29T06:04:26Z")

</div>

thank u russian for the reply.

i did add has\_and\_belongs\_to\_many :users. to User model.

---

<div class="post-metadata">

**Author:** ![gow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@gow](https://www.ruby-forum.com/u/gow)\
**Post date:** [June 29, 2007, 6:08am UTC](https://www.ruby-forum.com/t/need-some-info-regarding-how-to-add-friends/107998/5 "2007-06-29T06:08:26Z")

</div>

thank u shai

the urls u send were really helpful.i got great deal of relevant stuff  
from those links.

On Jun 28, 12:51 pm, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
