How to Create A Friendship Relation on Rails

How to Create A Friendship Relation on Rails hackernoon.com2 years ago in #Dev Love36

Engineering is about dividing problems into smaller partials. And then solving them with the best approach. As an engineer, I enjoy problem-solving. In this article, you will learn how to create friendships on Rails. In the most basic form, this is far away from perfection. But totally working. You can find the GitHub repository link at the bottom of the page to dive deeper into the code. In the repository, you will also find very simple session management. After reading this article you will have an understanding of friendships on Rails. When you want to create a rails app with user friendship interaction, you have to decide what your final product will be. There are many different ways to form a friendship model. In this project we are choosing this model: 1. Users are allowed to send a friendship invitation to other users. 2. Users can accept or reject the invitation that is coming from another user. Accepting -Will update a record column from false to true Rejecting -Will delete the record. 3. Unfriending will delete the record We will have a User model, and an Invitation model. Let’s focus only on friendship details. The invitation model will have only three columns(by the way, keep in your mind that the Rails is always giving you 3 more columns by default: id as primary key, and 2 timestamps). Those columns are: user_id, friend_id, and confirmed. user_id and friend_id are both referencing back to the users table(User model). When I am sending an invitation to my friend: user_id will be my id and friend_id will be my… well, you got that! And confirmed will be false by default. This means an invitation is starting to wait to be accepted or rejected at the moment it got created. When the invitation is rejected, the row is gonna be deleted. When it is accepted the row is gonna be updated, confirmed: from false to true. Any record with true value in the confirmed column means that those two ids are friends now. Any record with a false value in the confirmed column means that user_id is waiting for friend_id to accept/reject his/her invitation. Let’s run these three different commands on command line: $ rails new friends4ever $ rails g scaffold User name $ rails g model Invitation user:references friend_id:integer confirmed:boolean After this command we need to add to our migration file. Right after this line: Let’s go to our model files, first: appmodelsuser.rb Hold on there, I am explaining one by one… Our user has many invitations. This means A user object will respond to the “invitations” call. User.first.invitations =should make sense Our user has many pending invitations. Yes, try to stick with plain English, this will help a lot! This means the same thing, A user object should respond to the “pending_invitations” call. And we have options here. First option is a scope, this tells ruby to look for records only has “false” value on pending column. The other option is the “class_name”. We are adding this because rails can’t find the related class from the “pending_invitations”. So I am telling rails “Hey, I want this call on my object and go look for this in that class”. Rails going to the Invitation class and fetch all the records with specific user_id and confirmed values.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply