# Creating a "friends" association?

**URL:** <https://www.ruby-forum.com/t/creating-a-friends-association/92830>\
**Category:** Rails\
**Created:** [April 24, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/creating-a-friends-association/92830 "2007-04-24T19:56:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/creating-a-friends-association/92830/1 "2007-04-24T19:56:44Z")

</div>

Hi, I have a Friend model that looks like this:

create\_table “friendships”, :force =\> true do |t|  
t.column “sent\_by\_user\_id”, :integer  
t.column “received\_by\_user\_id”, :integer  
t.column “status”, :boolean  
end

I would like to be able to define a :friends association in my user  
model that returns friendships that are both sent\_by and received\_by a  
given user. Any way to do this?

I’ve been told that traditionally you just create two friendship  
records to handle this, but it seems like a cumbersome solution.

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/creating-a-friends-association/92830/2 "2007-04-24T20:01:43Z")

</div>

check out plugin acts as friends at agileweb.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2007, 2:18am UTC](https://www.ruby-forum.com/t/creating-a-friends-association/92830/3 "2007-04-25T02:18:21Z")

</div>

[I sent this a few hours ago but have not receive it back, please excuse if you got it twice.]

On Apr 24, 2007, at 6:13 PM, removed\_email\_address@domain.invalid wrote:

> model that returns friendships that are both sent\_by and received\_by a  
> given user. Any way to do this?
> 
> I’ve been told that traditionally you just create two friendship  
> records to handle this, but it seems like a cumbersome solution.

That’s the canonical solution if it’s OK for the amount of data,  
otherwise you need to emulate symmetry by hand:

sender.friends  
receiver.friends

should contain each other and the nice way to do that is to delegate  
in AR. That symmetry is then maintained by callbacks on the join model.

Additionally, you normally want to apply transitive somehow and the  
SQL is easier if you have a “direction” to follow, I wrote a little  
about this here:

[Advogato: Implementation of a network of contacts](http://www.advogato.org/article/914.html)

– fxn

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 25, 2007, 4:08am UTC](https://www.ruby-forum.com/t/creating-a-friends-association/92830/4 "2007-04-25T04:08:28Z")

</div>

On Apr 24, 2007, at 6:13 PM, removed\_email\_address@domain.invalid wrote:

> model that returns friendships that are both sent\_by and received\_by a  
> given user. Any way to do this?
> 
> I’ve been told that traditionally you just create two friendship  
> records to handle this, but it seems like a cumbersome solution.

That’s the canonical solution if it’s OK for the amount of data,  
otherwise you need to emulate symmetry by hand:

sender.friends  
receiver.friends

should contain each other and the nice way to do that is to delegate  
in AR. That symmetry is then maintained by callbacks on the join model.

Additionally, you normally want to apply transitive somehow and the  
SQL is easier if you have a “direction” to follow, I wrote a little  
about this here:

[Advogato: Implementation of a network of contacts](http://www.advogato.org/article/914.html)

– fxn
