Associations on the Daily

Luis Lozano
4 min readApr 1, 2021

As a new software developer learning Ruby on Rails, I decided to embark on a journey to help myself understand the mysterious Rails associations. What I have found out is that it is not mysterious at all. We deal with associations daily. When you go home which family will you go to? You will go to the one you belong to. Let us look at a few common Rails associations and how they relate to our daily lives then put them into codespeak.

I will begin with belongs_to and has_many since they work together. belongs_to is a one-to-one relationship that can only be used if it holds a foreign key in its class. Family members will usually all have the same last name. When someone needs to get information from you, they call you by your first name. In this way your first name is the foreign key that sets you apart from your family members. The important thing here is to remember to include your foreign key!

has_many implies a one-to-many association that works with the belongs_to association. It does not need a foreign key in its table. Remember to use belongs_to with has_many for the two models to communicate with each other.

Model set up for belongs_to and has_many associations.
Table setup for belongs_to and has_many associations. Note: even though we are referring to just the one of you, it is common rails convention to create tables with plural names.

The next relationship is has_many :through. It specifies a many-to-many relationship through a third model. Here we will divide our family into parents and kiddos. After several weeks of school and work, nothing brings parents and kids together like the holidays! Here is codespeak for how we would set up this association for something as common as family holidays.

Model setup for has_many :through associations.

To set this table up it would be like the ones above except this time the holiday table would get a parent id and a children id because of the belongs_to associations. In this way when our family goes down memory lane, they can reference a specific holiday to a parent or kid from that holiday and get that parent or kid’s information. In this case their name and age.

Table setup for has_many :through associations.

Another rails association is has_one :through. This association sets up a one-to-one with another model. One thing most of us can relate to is having a car garage or driveway. But human beings have faces, arms, and legs but not car garages! However, a person’s house can have a car garage. If that house belongs to a person and a car garage belongs to that house then that means the car garage belongs to that person. Confusing right? Well this relationship helps us clarify how a person can have a car garage, which is through their house. Here I will use your house that has a car garage as an example.

Model setup for has_one :through associations.
Table setup for has_one :through associations.

There are a few more associations that need to be explored such as has_and_belongs_to_many and has_one. However, the ones I talked about are a good starting point for us to learn. Associations in rails can seem very confusing. One of the most confusing things for me was where I needed to set my foreign keys (or ids). If nothing else just remember that anytime a class belongs to something that class needs the key to what it belongs to. On the flip side, if you are setting up a has_one relationship then the opposing class must have the foreign key.

As your apps get bigger your associations will become more complex. It helps to simplify things and relate to them while we build our skills first. Hope this helps!

--

--