# Some advice on DB modeling

**URL:** <https://www.ruby-forum.com/t/some-advice-on-db-modeling/48521>\
**Category:** Rails\
**Created:** [November 21, 2005, 8:58pm UTC](https://www.ruby-forum.com/t/some-advice-on-db-modeling/48521 "2005-11-21T20:58:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 21, 2005, 8:58pm UTC](https://www.ruby-forum.com/t/some-advice-on-db-modeling/48521/1 "2005-11-21T20:58:36Z")

</div>

I need some help designing my database. i am trying to create a  
community  
site very similar to MySpace, Freindster, etc. I want each of my users  
to be  
able to add other users as friends. I’m just not sure how to set up my  
tables.

I have a User object. I’m guessing I need some other table/model to  
handle  
the associations between the users. I created an Association model and  
said  
that User has\_many :associations and that Association belongs\_to User.  
But  
that doesn’t seem right.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 21, 2005, 9:22pm UTC](https://www.ruby-forum.com/t/some-advice-on-db-modeling/48521/2 "2005-11-21T21:22:47Z")

</div>

Ramin wrote:

> Any help would be greatly appreciated.  
> Try this.

CREATE TABLE `friends` (  
`user_id` int(11) NOT NULL default ‘0’,  
`friend_id` int(11) NOT NULL default ‘0’,  
KEY `user_id` (`user_id`),  
KEY `friend_id` (`friend_id`),  
CONSTRAINT `friends_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users`  
(`id`),  
CONSTRAINT `friends_ibfk_2` FOREIGN KEY (`friend_id`) REFERENCES  
`users` (`id`)  
)

then

class User \< ActiveRecord::Base  
has\_many :friends, :join\_table =\> ‘friends’, :class\_name =\> ‘User’,  
:foreign\_key =\> ‘friend\_id’

def add\_friend(friend)  
friends \<\< friend  
end  
end

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 21, 2005, 10:23pm UTC](https://www.ruby-forum.com/t/some-advice-on-db-modeling/48521/3 "2005-11-21T22:23:16Z")

</div>

Hi, thanks for your suggestions… however, I’m getting the following  
error:

Unknown key(s): join\_table

I can’t find the join\_table option in the has\_many documentation.

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [November 21, 2005, 10:39pm UTC](https://www.ruby-forum.com/t/some-advice-on-db-modeling/48521/4 "2005-11-21T22:39:28Z")

</div>

Sorry about that. It should be:

has\_and\_belongs\_to\_many :friends, :join\_table =\> ‘friends’, :class\_name  
=\> ‘User’, :foreign\_key =\> ‘friend\_id’

You can also access the inverse association with:

has\_and\_belongs\_to\_many :friends\_of, :join\_table =\> ‘friends’,  
:class\_name =\> ‘User’, :foreign\_key =\> ‘user\_id’,  
:association\_foreign\_key =\> ‘friend\_id’
