Friend requests made simple using javascript and a rails api
Published: 2020-04-04 22:46:18

Loads of modern apps employ some sort of friends list feature. The ability to create relationships among users has become a common-place practice that brings people together via technology. In this blog I am going to share what I believe is the simplest way to implement associations amongst users via friend requests and a friends list. 

As a student of Flatiron’s software engineering course, we are challenged with completing five projects during our 15 week program. Both my second and third projects implemented a friend’s list feature. I did a good deal of research on how to implement friends lists. Ultimately my group members and I were able to come up with a method that I believe to be the easiest to understand as well as code. I am using a rails api for my backend and vanilla javascript for my front.

This method uses two models: users and friends list.  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n17swz791jcvjxkjncc2.png)
Seen above is a diagram to help you visualize this relationship. The User class has two subclasses called as_receiver and as_requestor which act like separate models themselves. FriendRequest acts a join table between our two User subclasses. 

Inside of these models we can create their associations using these keywords: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3td3xi8qzeiro7ut5x8w.png)
As you can see there is only one User model, but when we define a relationship in this manner we get two subclasses associated between FriendRequest and User.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u5p5tl9vw3uwd92ettcl.png)

Once these relationships are created we have to add their IDs into the migration files:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f0f22ru2p4i0krjnw8sr.png)
Notice I didn’t add anything special into my CreateUsers table
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u4tocle5xtnumb4w46g6.png)
CreateFriendRequests is where the magic happens. We now have two User ID’s being associated through this table. The user that initiates the friendship is stored into the requestor_id slot while the receiver of that friend request is stored into receiver_id. Status will have one of two possibilities: “pending” and “accepted”. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nikkhs0i8rvf33dzf2dp.png)
This would be the logic behind an add friend button. A post request is made to create an instance of FriendRequest which associates the current user as_requestor with another user as_receiver. 
After that is done it doesn’t take much to show pending requests. Iterating through all FriendRequests(request=each iteration):
          ``else if ((request.requestor.id == u1.id || request.receiver.id == u1.id) && request.status == 'pending'){``

Accepting the request simply makes a patch request to our rails backend to change the status of the request to “accepted”
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2s5nh9gcx7oql3tmstj3.png)
Declining the request is even simpler as it just destroys that friend request altogether:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wgqnv5x7c1ave2vmsabl.png)
Once the request is accepted its as easy to show as it was to show pending requests.
``if ((request.requestor.id == u1.id || request.receiver.id == u1.id) && request.status == 'accepted'){``


I hope this blog is useful for anyone who is looking to implement a friends feature in their program!