Getting Associated with ActiveRecord and Rails.
One of my bigger challenges while learning Rails was remembering how exactly ActiveRecord Associations work. It should be simple right? If a User has_many
posts, the posts should belongs_to
a user, etc. But throw in a has_many :through
and other circumstances, suddenly the lines aren’t so clear. So, let’s take a dive into what exactly ActiveRecord Associations brings to the table and how that affects your apps.
In Rails, an association is a connection between two Active Record(AR) models. Associations are necessary when you want to have access to a model within the other. By declaring that these two models are together, we can streamline and lessen the amount of code you’re writing to make it DRY as well as acquire methods (things like .create, .build, etc) from AR to make your life easier. Thanks to AR, we can also use macros to be able to set these associations up:
Here I have my models and their associations. The important distinction is the joins table (when two belongs_to
are defined within one model) which is creating an association between both the User and Dcd through the Posts model. This is set up so that when a post is created, its creator and where it was posted will be identifiable through the foreign keys it receives in the database due to the association.
Another thing to keep an eye on is the has_many :through
association in the Users model. In this instance, I’m establishing a relationship from posts through dcds — so it needs to know which user and which dcd it’s attaching to. That way, when I create a dcd with my user, and add a post to it, I can find that post through a few simple queries in my console.
Utilizing user flow and charts are a great way to figure out how your associations work. When in doubt, draw it out.
How does all of this affect my app? Well, now that I’ve got my associations set up, I can go through my app creating routes, forms, methods, and whatever else I want to get a fully functioning website. In the code above, I’ve set an instance variable of @user_dcd
to an empty array, from there I collected all of the created Dcds inside of the @dcds
instance variable, then iterated through each of them and compared the dcds user ID to the current users ID. That way the current user can see only the dcds they’ve created.
After some refactoring, and my associations are correct, I should be able to narrow down my method to two lines of code!
Another cool way to see how associations affect your app are in your forms. When the POST request is made after filling in the form, the hidden_field
of “user_id” gets passed in upon submission. Despite this being a form for creating a new dcd, I still need to have a way to associate the user model to the dcd model (since a dcd belongs to the user) — which is why the hidden_field
is so useful.
Hopefully this helps whoever is reading this as much as it did me. Rails and Active Record are really powerful web development tools, which is no wonder why they’re so intimidating at first. If you ever get stuck, make sure to reference rails guides, and particularly their bit on associations!