# Some basic help on creating "friends" that are "users?"

**URL:** <https://www.ruby-forum.com/t/some-basic-help-on-creating-friends-that-are-users/194138>\
**Category:** Rails\
**Created:** [August 31, 2010, 1:07am UTC](https://www.ruby-forum.com/t/some-basic-help-on-creating-friends-that-are-users/194138 "2010-08-31T01:07:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 31, 2010, 1:07am UTC](https://www.ruby-forum.com/t/some-basic-help-on-creating-friends-that-are-users/194138/1 "2010-08-31T01:07:47Z")

</div>

New to RoR, trying to model this correctly (which should be super simple  
I’d  
think, but I’m stuck at the moment):

Any given “User” of the app can have a group of “friends” (where friends  
are  
also users.) (think typical facebook friends concept.)

I know I should have a ‘user\_friends’ table that has two user\_ids as fks  
to  
the User table one for the user and the other to hold multiple ‘friends’

My first shot is looking like this…

class User \< ActiveRecord::Base  
acts\_as\_authentic  
has\_many :user\_friends, :dependent =\> :destroy  
has\_many :friends, :class\_name =\> “User”, :foreign\_key =\> “friend\_id”,  
:through =\> :user\_friends, :dependent =\> :destroy  
end

class UserFriend \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :user, :class\_name =\> “User”, :foreign\_key =\> “friend\_id”  
end

#migration  
class CreateUserFriends \< ActiveRecord::Migration  
def self.up  
create\_table :user\_friends, :id =\> false do |t|  
t.integer :user\_id  
t.integer :friend\_id  
t.timestamps  
end  
end

When I run my test, it barfs saying

Failures:

1. User should create a user and some friends  
Failure/Error: user.friends \<\< f1 \<\< f2  
Could not find the source association(s) :user\_friends in model  
UserFriend.  
Try 'has\_many :friends, :through =\> :user\_friends, :source =\> '.  
Is  
it one of :user?

Where my test looks like:

it “should create a user and some friends” do  
user = Factory.create(:user\_rick)  
f1 = Factory.create(:user\_rachel)  
f2 = Factory.create(:user\_fred)  
user.friends \<\< f1 \<\< f2

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 31, 2010, 4:24am UTC](https://www.ruby-forum.com/t/some-basic-help-on-creating-friends-that-are-users/194138/2 "2010-08-31T04:24:00Z")

</div>

On Mon, Aug 30, 2010 at 7:06 PM, Rick R [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :user\_friends, :dependent =\> :destroy  
> has\_many :friends, :class\_name =\> “User”, :foreign\_key =\> “friend\_id”,  
> :through =\> :user\_friends, :dependent =\> :destroy  
> end
> 
> class UserFriend \< ActiveRecord::Base  
> belongs\_to :user  
> belongs\_to :user, :class\_name =\> “User”, :foreign\_key =\> “friend\_id”  
> end

Solved, I had to change UserFriend to:

class UserFriend \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :friend, :class\_name =\> “User”, :foreign\_key =\> “friend\_id”  
end

(Yes, I’ll remove the unnecessary foreign\_key declarations as well.)
