Ruby On Rails: ActiveRecord Associations
During my time at bootcamp, I had a hard time understanding ActiveRecord and associations. I would end up thinking of real live scenarios and then confusing myself with multiple possibilities of what those object relationships could be. But instead of looking at the overall model, the best way to go about understanding the relationships between each model is to look through the lense of the model.
I will be using Instagram to help explain ActiveRecord associations. I have three models: User, Post and Comment. Now, since this app is User-focused, let’s make that the center focus of how we build out the models. You might be wondering “well, what do you mean this is user focused?” Instagram is user-focused because you can’t post or comment without having an account. Therefore, nothing else exists without the User. The User model is the only one that can exist without depending on another model in this particular application.
The next model we will work with is the Post model. Let’s go back to the time when Instagram only allowed you to be signed into one account at a time. Now that our User Joyvincent is signed in, she can make as many posts as she wants. All these posts that she created belong to one User, Joyvincent. This is an example of a one to many relationship. The User is the one and the Posts are the many. Here is a visual representation of the relationship.
As you see here, the User Joyvincent has many Posts. These Posts only exist because of Joyvincent. Therefore, if you were to get rid of the User who is the foundation of this relationship, there would be no more Posts.
Now you might be wondering, where exactly does the Comment model fit into this ERD (Entity Relationship Diagram). On Instagram, a User can create comments under a Post. In this ERD below, I am zooming in on one Post. Under a single Post there can be multiple comments by the User. This is another example of a one to many relationship. You can see that in the entire model the user has many comments and a Comment as one User.
Now that we know what the models do, it’s important that I also cover foreign keys. The foreign keys live in the table of the model that has the “belongs to” relationship. Comments belongs to both the Posts and the User. Post belongs to a User therefore the user’s id will be in the Post table. When your writing this in, make sure you are putting user_id: integer
in the generator.
Knowing the dependencies of each model will help in deciding what order to create them. In these models we have been working on, you would want to create the User first, Post second, Comments third. Note: If you want to create a project with these model names, feel free to do so! Just keep in mind that POST is also a verb in regards to routes.
Relationships can be hard, even in coding! Just like in our daily lives, understanding the many relationships we have can be overwhelming. With these ActiveRecord relationships, it’s a lot easier breaking it down to one or two models and then building from there.