Throughout my coding journey, when learning something new, I always wondered how something I am learning now as a new software engineer has implications on what I already know as someone who has been on the internet practically my whole life. It blew my mind to be able think that I understand where something that I couldn't even fathom a few months ago, I can now see how different websites accomplish their goals.
Now, currently learning and working with creating my own back-end databases, a conversation with a friend on the matter led me to ask myself: "Now that I'm working with databases, do I know how social media giants such as Facebook and Twitter work ? How do databases of hundreds of millions of users keep track of who is friends with who ?"
Just these past few weeks I have starting about relational databases and the kinds of relationships different table of information have with one another. Usually by having a table of one kind of Data, for example "grocery items" and another table with a different kind of data, for example "customers" joined with a third kind of table that "joins" two different kinds of "Class instances" and creates a "many to many" relationship. See figure below.
Each "order" belongs to a "grocery item" and to a "customer" while each "customer" has many "orders" and through "orders" has many "grocery items" and similarly, each "grocery item" has many "orders" it can be in, and through the many "order" has many "customers" as well.
This naturally brought up the question for me, what if I want to be friends with a human, and not a Jicama ? Do we need a second table of humans, just for me to be friends with them ? Would they be anti-humans ? Food for thought.
Thankfully we can have something completely different !
Using ActiveRecord, we can make a join table model that joins two instances of the same class, allowing for human-to-human friendships in this case. The power behind this is obvious as it creates the opportunity to create relationships between two instances of the same class, without much issue.
The domain model would look something like this:
Here we have a model where a "friendship" belongs to two instances of "human" and each "human" has many "friendships" and through them has many other "humans". The syntax for setting this kind of relationship is a little different than usual, and has some nuances depending on the kind of relationship between to "humans" that will be the subject of future posts. This just serves to provide some insight into what is possible and some food for thought.