# Has\_many association. Is this possible?

**URL:** <https://www.ruby-forum.com/t/has-many-association-is-this-possible/150068>\
**Category:** Rails\
**Created:** [October 3, 2008, 4:21am UTC](https://www.ruby-forum.com/t/has-many-association-is-this-possible/150068 "2008-10-03T04:21:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@TheZ](https://www.ruby-forum.com/u/TheZ)\
**Post date:** [October 3, 2008, 4:21am UTC](https://www.ruby-forum.com/t/has-many-association-is-this-possible/150068/1 "2008-10-03T04:21:56Z")

</div>

I have the following schema.

table: user  
t.column :id, :integer

table: friends  
t.column :user\_id, :integer #Owner of this friendship  
t.column :friend\_id, :integer #FK to User table

table: activity  
t.column :user\_id

Basically I want to display all activity from friends that i have

I currently have the following has\_many finder…

has\_many :friend\_activities,  
:class\_name =\> ‘Activity’,  
:finder\_sql =\> ‘select a.\* from activities a, friend f where  
a.user\_id = f.friend\_id and f.user\_id = #{id}’

Is it even possible to change the above has\_many to not user  
the :finder\_sql but using some combination  
of :through, :source … ??? If so can someone please help.

---

<div class="post-metadata">

**Author:** ![TheZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@TheZ](https://www.ruby-forum.com/u/TheZ)\
**Post date:** [October 3, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/has-many-association-is-this-possible/150068/2 "2008-10-03T15:13:58Z")

</div>

I’ll be making a few assumptions here since your full schema is not  
shown here, but this is what I can deduce from your description:

1. You have a reflexive many-to-many relation from User back to User  
though Friend.
2. Friend has it’s own model class so has it’s own identity (and id  
column).

table: friends  
t.column :id, :integer # \<-- I added this here for clarification  
t.column :user\_id, :integer #Owner of this friendship  
t.column :friend\_id, :integer #FK to User table

Given both 1 & 2 above are true, you could add another many-to-many  
relation for Friend and Activity through FriendActivity:

table: friend\_activities  
t.column :friend\_id, :integer #FK to friends table  
t.column :activity\_id, :integer #FK to activities table

I didn’t put a ton of thought into this, but I believe it’s the approach  
I would try first.

TheZ wrote:

> I have the following schema.
> 
> table: user  
> t.column :id, :integer
> 
> table: friends  
> t.column :user\_id, :integer #Owner of this friendship  
> t.column :friend\_id, :integer #FK to User table
> 
> table: activity  
> t.column :user\_id
> 
> Basically I want to display all activity from friends that i have
> 
> I currently have the following has\_many finder…
> 
> has\_many :friend\_activities,  
> :class\_name =\> ‘Activity’,  
> :finder\_sql =\> ‘select a.\* from activities a, friend f where  
> a.user\_id = f.friend\_id and f.user\_id = #{id}’
> 
> Is it even possible to change the above has\_many to not user  
> the :finder\_sql but using some combination  
> of :through, :source … ??? If so can someone please help.
