DEV Community

Cover image for Associations: Rails Many-to-Many Relationships
Tahjg Dixon
Tahjg Dixon

Posted on • Updated on

Associations: Rails Many-to-Many Relationships

When programming in Rails, there is numerous relationships to learn. One of the most common is the Many-To-Many relationship. There's no question if you program in Rails, this is a association you must know. A Many-to-Many relationship consist of utilizing the has_many :through macro. For this to work correctly, you will need two models and a join model. In our example models, we will use Student, House, and Sorting_hat as our join.

Yes, today we're going to be using the world of Harry Potter as our example!

#For less confusion involving our model classes
#The Syntax for inheriting Application Record is:

class Class_name_here < ApplicationRecord 

Enter fullscreen mode Exit fullscreen mode

Our Many-to-Many relationship goes something like this BELOW:

class Student < ApplicationRecord
  has_many :sorting_hats
  has_many :houses, through: :sorting_hats

class Sorting_hat < ApplicationRecord #JOIN MODEL
  belongs_to :house
  belongs_to :student

class House < ApplicationRecord
  has_many :sorting_hats
  has_many :students, through: :sorting_hats
Enter fullscreen mode Exit fullscreen mode

Although there's only one sorting hat in the world of Harry Potter. For our example today, there's a sorting hat for each individual student. Our join table will always belong_to our other models individually (singular).
Image description
You can think of our join table as the bridge between the other two models. The only way for our House and Student models to interact is through the sorting_hats join model. Without our join model, there's no connection between our House and Student associations. In other words, Each Student(singular) goes through the Sorting Hats and is assigned a House. Once each Students travel through the sorting hats, you are then assigned to be apart one the amazing Hogwart Houses! Gryffindor, Hufflepuff, Ravenclaw, and.... Slytherin.

The Sorting_hat model is the join model in this instance. Lets establish each relationship here below more clearly.

  • The Student has many sorting_hats and many houses through sorting_hats.

  • The House has many sorting_hats and many students through sorting_hats.

  • The Sorting_hat belongs_to student (each individual student) and belongs_to House (each individual house).

Associations in ruby on rails can be very confusing. Next time you come across a bump in the road, think of a real life scenario associations can be compared to. I promise It will shed light on how simple associations can really be. For example, If you are given a set of models. These models are Doctor, Patient, and Appointments. The Doctor and Patients cannot interact without any Appointments. Which means Appointments will be our JOIN model. The JOIN model creates the Many-to-Many relationship. That is one example to think of Many-to-Many relationships. Come up with examples yourself to make things a bit more clear. Draw these relationships down on paper or create diagrams.

I hope this helped you get a better understanding about Many-To-Many Associations in rails. Happy Coding Everyone!

Top comments (1)

njoroge profile image
Dennis Kamau

Thanks for sharing.

I now understand it better