A Simple Twitter App with Ruby on Rails – Building Friendships
Introduction
This is the third and final part of series on how to create a twitter style web application with Ruby on Rails. This part will cover how to add friendships between users.
- The first part of this tutorial: A Simple Twitter App with Ruby on Rails – Messages With Ajax
- The second part of this tutorial: A Simple Twitter App with Ruby on Rails – User Authentication
Self-Referential Relationship

To create friendships between users we have to deal with a special type of association, which is called a self referential relationship. It is called this because the model (in this case, User) references itself. Why? Well if a given “user” has many “friends”, those “friends” are also “users”. Furthermore, each of those friends can also have friends, so we are dealing with a many to many relationship.
The relationship can affectively be called a “friendship” because friendships can be gained and lost (as in real life). So, let’s create the friendship model with two foreign keys.
> ruby script/generate model friendship user_id:integer friend_id:integer
Now, migrate the database:-
> rake db:migrate
Making and Losing Friends

We will need to create and destroy friendships and for this we will need a controller for friendships:-
> ruby script/generate controller friendships
Now add the create and destroy methods as shown below:-
class FriendshipsController params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to root_url
else
flash[:error] = "Error occurred when adding friend."
redirect_to root_url
end
end
def destroy
@friendship = current_user.friendships.find(params[:id])
@friendship.destroy
flash[:notice] = "Successfully destroyed friendship."
redirect_to root_url
end
end

So, what exactly do we relate the user model to?? Well, first we need to specify that the friendship model belongs to a friend (which is actually a user!). You can do this by adding some more lines to the user model:-
class Friendship "User" end
We need to add two lines to the User model. A User has many friendships and has many friends through friendships. This reads almost exactly as it is coded, which is a testament to Ruby on Rails.
class User :friendships ... end
Listing your Friends

If we want to list all the registered users and allow the current user to befriend other users, then we will need to create a new view in the users folder called index.html.erb:-
user), :method => :post %>

Now, let’s put the controller actions in place. Open the users_controller file and add the index and show methods.
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = current_user
end
...
end
We need to do two more things before we can give this a whirl. First add the friendships resource to the routes file:-
map.resources :friendships
…and finally, we can add some links on the posts/index.html.erb file:-
... "users", :action => "show", :id => current_user } %>
Ok, we can now start up the server and browse to http://localhost:3000 to have a look.

Summary
Obviously there are several directions you could take this application. A lot of features could be added and it could be spruced up, somewhat. I hope these tutorials have helped you in some way and again, I would highly recommend that you try Ruby on Rails, if you haven’t already.
Author: Phil McClure
Phil McClure is a Software Developer from Belfast, Northern Ireland. His main interests are software architecture, design patterns and how these can be applied to web development. Phil blogs at Therailworld. Follow him on Twitter.
Related posts:

