Adding a friendship request action

Hi there! Any help abou how i can do a system of friendship request? Add a friend and that friend accept the request. Thanks for any help!

Posted over 5 years ago by Eduardo Gomes Martins
Posted over 5 years ago by Alex Yang

This is a pretty complex problem, but I'll help you think through the database piece, which is the hardest part. I'm assuming you already have a database of Users and now you're trying to figure out how to connect them to each other. There are a few ways to do this, but since you want to build friendships through friend requests, it's best to take the view that friend requests aren't symmetrical. In other words, it matters who the initiator is and who the recipient is.

I would start by creating a new database with two columns: active_user_id and passive_user_id. Each friend request will be stored as a row in our database. So when User1 sends a friend request to User2, active_user_id=1 and passive_user_id=2. When User2 confirms the request, a second row will be created with active_user_id=2 and passive_user_id=1. To create this database, you should run this command:
rails generate model Friendship active_user_id:integer passive_user_id:integer

Then run 'rake db:migrate' and restart your Rails server.

With this database created, we now need to think about how to set up the associations between the User and Friendship models. It's best to think about this as a triangle between the Active User, the Passive User, and the Friendship. There are three separate associations to consider:
1) Active User - Friendship
2) Passive User - Friendship
3) Active User - Passive User

1) Let's start with the association between the Active User and the Friendship. Since this Friendship was initiated by the Active User, we'll call this type of Friendship an Active Friendship. In this case, the Active User has many Active Friendships and an Active Friendship belongs to an Active User. In our user.rb model, we'll write:
has_many active_friendships, class_name: "Friendship", foreign_key: "active_user_id", dependent: :destroy

Here, we're telling our app to look in our Friendship database and every time it sees the User's id appear in the active_user_id column, it should recognize this as an Active Friendship. The 'dependent: :destroy' tells our app to destroy all of a User's Active Friendships when the User itself is deleted.

Then in our friendship.rb model, we'll write:
belongs_to :active_user, class_name: "User"

Note that we're using 'class_name' here so our app knows that we're using the name 'active_user' to represent a type of User.

2) Now let's look at the association between a Passive User and the Passive Friendship. In our user.rb model, we'll write:
has_many passive_friendships, class_name: "Friendship", foreign_key: "passive_user_id", dependent: :destroy

This time, we're telling our app that every time the User's id appears in the passive_user_id column, it should recognize this as a Passive Friendship.

Then in our friendship.rb model, we'll write:
belongs_to :passive_user, class_name: "User"

3) Now for the association between the Active User and the Passive User. This is a 'has_many :through' association - in other words, the Active User is connected to the Passive User through a Friendship. (You can read more about this type of association here: So in our user.rb model, we'll write:
has_many active_friends, through: :active_friendships, source: active_user
has_many passive_friends, through: :passive_friendships, source: passive_user

This distinguishes between active friends who a User has requested and passive friends who have sent a friend request to the User.

This is all pretty complicated, but I hope this way of breaking down the different components makes sense. Let me know if it helps!