# Trying to Create a Friend System

**URL:** <https://www.ruby-forum.com/t/trying-to-create-a-friend-system/108116>\
**Category:** Rails\
**Created:** [June 29, 2007, 4:50am UTC](https://www.ruby-forum.com/t/trying-to-create-a-friend-system/108116 "2007-06-29T04:50:21Z")\
**Posts on this page:** 2\
**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:** [June 29, 2007, 4:50am UTC](https://www.ruby-forum.com/t/trying-to-create-a-friend-system/108116/1 "2007-06-29T04:50:21Z")

</div>

I’m trying to add the ability to add other users of my app as friends  
(to follow their recent activity, etc.), however I can’t grasp what  
ActiveRecord association should be used. Basically, I’ve tried to do  
this:

class User \< ActiveRecord::Base  
has\_one :user\_picture  
has\_many :scores  
has\_many :friendships  
end

class Friendship \< ActiveRecord::Base  
belongs\_to :user  
end

And my friendships table looks like this:  
create\_table :friendships, :force =\> true do |t|  
t.column “user\_id”, :integer, :null =\> false  
t.column “friend\_id”, :integer, :null =\> false  
end

User\_id holds the ID of the user that added another user as friend,  
and friend\_id is the ID of the user that was added as a friend (sorry  
if this sentence didn’t make much sense).

All I need to do is track who is friends of who, and see if their  
friendship is mutual. If I keep working on it, the code above can  
probably work, but it doesn’t seem to Railsy. So if you can, please  
let me know how you would pull this sort of thing off.

Thanks for taking a look,  
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:** [June 29, 2007, 9:02am UTC](https://www.ruby-forum.com/t/trying-to-create-a-friend-system/108116/2 "2007-06-29T09:02:43Z")

</div>

On 29 Jun 2007, at 03:49, TeeC wrote:

> end
> 
> Tony

I’m no guru on Railsyness, but I wonder if the tables you describe  
are capturing all of what’s happening in your relationships. Your  
last paragraph implies that friendship is mutual – and thus exists  
only when both requested and accepted, requiring two steps to create.  
As I read this, the friendships table holds both requests and  
accepted friendships. Separating them might make this easier and more  
transparent to manage…
