Active Record Associations: Has_many, Has_many, through: & Belongs_to

Jenny Lai
4 min readMar 7, 2017

Rails is a web application framework for developers that helps simplify common, repetitive tasks by abstracting them away. The framework emulates the same mannerism as Ruby. Out of the box, these abstractions are a time and sanity saver. However, there is some logic that is not abstracted away. When you build a Rails application and have multiple models, Rails doesn’t know the relationship between your models. You have to declare the relationships between your models.

Active Record Associations in Rails allows you to establish these relationships and connect models together. When you create an association, you place the foreign key of one model in another to link your models together. This interconnectivity ties back to your database. Your model is linked to your database when you have the model inherit from ActiveRecord::Base.

Through association, Active Record is able to store and retrieve that interconnected data.

Declaring your associations between models will also help streamline revisions and refactoring. It eliminates the pain of parsing through code from multiple models to update a piece of association code.

There are 6 types of associations in Rails:

  1. has_one

2. has_one #singular_model :through

3. belongs_to

4 has_many

5. has_many #plural_model :through #join_model

6. has_and_belongs_to_many

I will focus on the most commonly used associations: belongs_to, has_many and has_many :through. Associations in Rails is declared through a meta-style calling.

Belongs_to Association:

The belongs_to association establishes a one-to-one relationship with another model and thus the association is singular. This association implies that one instance of the declared model belongs to one instance of another model.

For a belongs_to association, you are placing the foreign key of the singular model (owner) you are declaring the belongs_to association on the table of the model (pet) you are declaring this belongs_to association in. In the above example, pets and owner are associated through a foreign key of owner_id, which is on the Pets table. With this association, you can find the pet’s owner by pet.owner. This will fire the below SQL query —

Rails will look for a foreign key of owner_id on the Pets table to return the owner of that particular pet.

Has_many Association:

The has_many association is a one-to-many relationship with another model. This association implies that one instance of the declared model has many of instances of another model, thus the associated model is pluralized. Along with a has_many association, there is usually a belongs_to association with the other model.

When you declare a has_many association, the foreign key would be on the same table and model in which you declared your belongs_to association. With the given example above, the foreign key: owner_id will remain on the Pets table. With the has_many association, you can find the owner’s pets by owner.pets, which will fire the below SQL query —

Rails will look for a foreign key of owner_id on the Pets table to return all of the pets that belong to that particular owner.

Has_many, :through Association:

The has_many, :through association is a many-to-many relationship with another model. This also sets up a join model for the two models associated with the many-to-many connection. It implies that one instance of the declared model has many of instances of another model, through the join model. The join model that is being declared with the has_many association is pluralized within the model that has the declaration and is the through model. For whichever models that the join model belongs_to, it inversely has a has_many association of that join model within those belongs_to models. For better clarification —

The :through join model, allows for a nested has_many association. With a has_many, :through association, there would be two foreign keys. The foreign keys would be of the models on which the has_many associations are declared on and on the table of the join model. For the above example, the join model is Pets. The Pets table would have a foreign key of the owner (owner_id) and vet (vet_id). You can find the vet’s pet owners by vet.owners, which will fire the below SQL query —

For more on Active Record associations in Rails, check out the documentation.

Also, check out the documentation for Active Record query methods.

--

--