How Social Media Connects You to Your Friends, and How You Can do the Same in Your Projects.

Social media comes in all shapes and sizes these days, and although they have different rules for how they handle things like friends or followers they have one thing in common. They all have a way for you to connect your account with the accounts of other users, after all, that’s what makes them “social”.

When I started a project to build a simple chat app, similar to what already exists on most social media apps today, but almost exactly like facebook messenger, where you can add friends and message them directly or in a group I quickly realized I had a small problem.

I knew how to create and save user data in a database, and I knew how to create models that represented my conversations and my messages and how to relate all of that data together to create the app but there was one thing I didn’t know how to do.

How was I supposed to take my data in the User table and relate it to itself… how could I relate one column(user) to another in the same table? I knew there must be a way. What I found was this; self join tables. It seemed very intimidating at first but was actually pretty simple once I understood.

The first step, as shown above is to create a join table like you would to join any two tables together. The difference here is that both foreign keys were creating relate to the primary key in the Users table. Since unlike when we create a join table for two separate tables we cant use the table name as a foreign key (we can’t have a join table with “user_id”, “user_id”), we have to tell our model what these foreign keys relate to. Like this:

here we specify that the follower_id relates to the class of user and so does the followed_id. Next we have to set up our User model:

now theres a lot there but lets take a closer look at the part were interested in:

These lines connect everything together so that a user can both have followers and follow other Users. This was all it took to set up the database and my models for my app to be able to have friends. All that was left was how to actually implement it in my controller which was just the same as if the table were two separate tables, instead of one.

I wanted to make sure that my users were friends with each other and not a one way relationship, which just meant that when Friends were added they were shoveled into each others followers and not just one.

for example if John wanted to be friends with Steve that might look something like this:

But that being said implementing a system more like instagram or twitter where you can follow a user who doesn’t follow you back would be even simpler because you would only need to update one users followers and wouldn’t need any verification like a friend request!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store