Many to Many Relationship - Ruby

Many to Many Relationship - Ruby

This is in continuation of my previous article. Many to Many relationship which is also called the Has-Many-Through relationship, exists between two objects and occurs when a single instance of an entity, relates to many instance of another entity and vise versa. As an example we can take doctor and patient entities.

When we examine their relationship, we notice that a doctor can see many patients and a patient also can be seen by many doctors. In this relationship, unlike the belongs_to /has_many relationship, it is difficult to put a reference of one object in to the other, as one doesn't solely belongs to the other object. In this case, to create a relationship between the entities, we need another entity that can hold a single instance of both objects. We call this a Join Entity. For our case, let us create an Appointment join entity.

No alt text provided for this image

The entity diagram is a bit high level explanation of the has-many-through relationship, which contain active record macros, (I will discuss it in my next article).

So as seen in the diagram, an instance of an appointment is surely belongs to a doctor and also to a patient. Using this joint table we can create an indirect relationship between a doctor and a patient, which enable us to get useful information out of each. If for example a doctor want to know the list of his patients, we can directly find that information through the appointments table. Same with patients, they can retrieve the list of their doctors, by going through the join table.

Let's view them using ruby codes.

No alt text provided for this image

A Patient and Doctor classes are instantiated with their attributes, up on initialization. Because of the join class Appointment, see below, we are able to create new behaviours/methods like new_appointment:- that enable both classes to hold new appointments, appointments:- to view list of their appointments, and more like, a doctor is able to see list his patients and vise versa.

No alt text provided for this image

The Join class, Appointment, belongs to both classes (through the macro, attr_accessor) and has the patient and doctor instances captured for every instance of an appointment. Those are being pushed to an array class variable. Which enable us to iterate over it, whenever we want an information about each entities.


The previous and this article shows a very low level but basic explanation of the belongs_to/ has-many and the has-many-through relationship. If we are able to correctly map the relationship between any real world objects, we can capture a dynamic data/information that will enable us to solve so many real world problems.


To view or add a comment, sign in

More articles by Hanna Mulugeta

  • Object Relationship in Ruby

    My Journey of becoming Software Engineer has started a few months ago when I decided to join Flatiron School’s…

Others also viewed

Explore content categories