# Problems w/ User having many friends

**URL:** <https://www.ruby-forum.com/t/problems-w-user-having-many-friends/157771>\
**Category:** Rails\
**Created:** [January 15, 2009, 5:25am UTC](https://www.ruby-forum.com/t/problems-w-user-having-many-friends/157771 "2009-01-15T05:25:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [January 15, 2009, 5:25am UTC](https://www.ruby-forum.com/t/problems-w-user-having-many-friends/157771/1 "2009-01-15T05:25:13Z")

</div>

This seems so easy but I keep hitting a brick wall on this.

I have this modeling:

```auto
 class User < ActiveRecord::Base
   has_many :friendships
   has_many :friends, :through => :friendships #...
 end

 class Friendship < ActiveRecord::Base
   belongs_to :user
   belongs_to :friend, :class_name => 'User', :foreign_key =>
'friend_id'
end
```

I have these 2 records in the friends table

user\_id, friend\_id  
3 4  
3 5

Now when I try to select all friends that belong to User of id 3, using  
this select I get one record instead of 2:

@users = User.find:all, :include=\>[:friendships],  
:conditions=\>[“friendships.user\_id=?”,3])

Any ideas how I can model this and query this correctly to get both  
records belonging to User 3?

Thanks a whole lot!

Clem

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [January 15, 2009, 11:18am UTC](https://www.ruby-forum.com/t/problems-w-user-having-many-friends/157771/2 "2009-01-15T11:18:09Z")

</div>

I’m not an expert, but:

Try finding the User.friends instead of passing conditions manually.

@users = User.friends.find(:all)

I’ve never used the “:through” like you did with “friendships”  
If the code above doesn’t work try this:  
{I guess this is the syntax based on the “through relationship”)

@users = User.friendships.friends.find(:all)

You can do some edit in the route file, and see waht you want by  
accessing this URLs:  
(doesn’t need controller stuff)

[example.com/users/3/friends](http://example.com/users/3/friends)

or

[example.com/users/3/frienships/friends](http://example.com/users/3/frienships/friends) (again, I don’t know about the  
“:through” thing)

The code should be:

map.resources :users, :has\_many =\> :friends, :through =\> :friendships

or just, I don’t know:

map.resources :users, :has\_many =\> :friends

Thanks
