Active Record Associations: Simplified
Tags: ruby, activerecord, beginners, webdev
Published: 2022-06-03 12:46:47

When learning Ruby, I felt a little too confident through learning about methods, hashes and all of the other basic commands and functions of Ruby. That was, until I began to learn about active record. Active record is an incredibly helpful and necessary piece of learning Ruby to create your backend database and being able to fetch and communicate with your backend from your front end application. 

**What is Active Record?**

Active Record is an ORM, which stands for Object Relational Mapping. ORM's are used to make fetching and representing data easier, and it saves us the time of writing out our own ORM which is always a plus!

**Active Record Macros**

Moving along to Active Record associations, these are commands held within active record. These commands create connections or links between your Ruby methods. These associations are as follows...

```
belongs_to :method
has_many :method
```

These two associations will cover almost all of the associations you will do in Ruby. A simple example of how to use these would be if you had two methods, one that carried the functionality for a tv and it's channels. You only have one tv, but the tv has many channels. So it would look something like this...

**TV**

```
has_many :Channels
```
**Channels**

```
belongs_to :TV
```

This functionality would be built out for a one-to-many relationship. The other relationship you will see is a many-to-many relationship. In, this example we will use video games and their respective players. One game may have many players, but one player may have many games. In this instance, the associations would look like this...

**Player**

```
has_many :games
```

**Game**

```
has_many :players
```

When it comes to learning something, it is always better to take it slow. As I learned these associations, I spent a lot of time working with just the belongs_to because it was the easiest to remember. It's one to one, so the simplest number of ideas to have swirling in your head at one time. One-to-many relationships were next and they are both really simple

I soon after moved to using the one to many relationship and even at times was using the "one to many, through" relationship. in which two methods would have the "has_many" with a third method that has the "belongs_to" method to each of the first two. This becomes more complicated, but is again essential to the understanding of Active Record in Ruby. 

**Has many -> Through**

Lastly to show the "has_many, through" relationship. This is when three or more methods are associated with each other with at least two main methods and a third that belongs to both. It is easiest to think of a triangle form for this hierarchy. Two main methods at the top side by side, both with a line drawn downward to the single method below. The method below has two belongs_to associations to the ones above it, and the two methods above have a "has_many" relationship with the single method below. 

The "has_many through" association would be used in this situation to link the two upper methods. So before the association, the two upper methods both have many of the lower method, but they don't interact or have access to the data in each other. so in this case we could use this...

```
has_many :games, through: :players
```

This is a simple example but in this case, we are saying that this unnamed method, has many games because it is routed through the players method, which already has access to the games method. 

**Conclusion**

Active Record is not entirely difficult or hard to learn, but it is still a vital piece of learning how to code with Ruby. Unless it is replaced by something once we move on to Rails, then I would assume we will be using this methodology for a good long time. I hope this helps, even just a little, someone who is struggling to understand or wants a better grasp on Active Record associations. 

Happy Coding!!!