Relationship Models in Ruby
Tags: ruby, database, oop
Published: 2020-05-24 17:33:34

Coming into Flatiron's Software Engineering program the biggest topic I struggled with was relationship models and how it all came together. As I quickly realized having a good grip on this is probably one of the most important topics in mod1. In an effort to try to help as many future cohorts that struggle with this topic and anyone trying to learn OOP, ORM's or ActiveRecord I'm going to go over some models and tips on how to read a README and be able to clearly understand the relationships. Let's begin!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3ia2ywlbkngc1eiqij99.gif)

Before you begin coding I cannot stress it enough to have a notebook nearby or as I will be using a virtual whiteboard that can be found [here] (https://awwapp.com).


###*One-to-Many*
One of the first models you will come across is the one-to-many model. With this model in some README's you will get something along the lines of "A **classroom has many students** but a **student has only one classroom**" some keywords to look out for are "belongs to" or "can only have one". So you may be asking yourself how do we translate this into code, well let's begin with our handy notebook first. We are going to use the "Student" and "Classroom" model.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j2iqrbjkoqohof47ptm0.png)

Let's say we initialize with our classroom class only having a "room number" and our student class with a "grade" and a "name". 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qta4k23xjdpg0lg0fc4u.png)

As seen above I've already drawn out that a classroom can have many students. So what's next? well, we know our student class can only have one classroom so who inherits what here? 

First I'm going to show you the wrong way of doing this and then explain why you should never do this. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v2pk0q53yb6diixmujoq.png)
Doesn't look too bad right? well what if we need another student and another student just got enrolled for your class as I'm typing this, it would look something like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tcjelm510q5aic5md708.png)

Keep in mind this is just one instance of a classroom what if a less popular classroom had fewer students.

This not only would break our program but cause a huge amount of bugs in our database. You can see that the program would break by us just trying to initialize it. So what's a good rule of thumb to go by? when speaking to one of my coaches he gave me a tip that stuck with me and it is.

*"in a parent-child relationship, the child will always have a reference to the parent. Kind of like passing genes. The parent is what has many of something the child belongs to a parent. The child will always have a reference to the parent."*
 
So what would be the correct way of doing this? 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rbrrb7q8zt2648mmc6zw.png)

Every instance of a student would be initialized with the classroom_id as it's inheriting from our parent class. 

###*Many-to-Many*

So let's say our app is getting an update now a student can have many classrooms so first off some keywords to look out for in this relationship is "A classroom *has many students* and a *student can have many classrooms*" let's start again with the empty whiteboard on how this would look.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j08m1a2lrb96tzqm73z4.png)

Once again I'm going to show you the wrong way of going about this first.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/95sysz22fbxns4xhic7m.png)

Doesn't look too bad right? but as stated in the README "have many" both can have many of each other. So, in reality, our program would look something like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sc9frd86mu9j0x94h9x1.png)

Once again breaking our program and our database. So how do we fix this? 
We add a join table/class. A table or class that will keep just track of this for us. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/76pqfwufivpoj2n2epns.png)

So now every time an instance of a student that has multiple classrooms or vice versa it will be initialized there with no problems and zero bugs. This can get very deep and you can have multiple join classes for different occasions but once you get the basic understanding of why things are placed where they are, it will make your job a whole lot easier. I hope this helps :)!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dtukxsff4vrnz6z06v5o.gif)