Active Record Associations
Tags: ruby, beginners, programming, tutorial
Published: 2022-12-06 05:26:55

Associations are the connections between the two models of Active Record. Without associations, programmers would have to write more code to do the same thing that can be done with less code with associations. We will be discussing the two types of Active Record Associations. They are what structure our code to make things not only simple to write but also simple to read. The two types are:
  
   * One to Many
   * Many to Many

The first one that we will be discussing is the One to Many association. This one is the simpler one of the two. In a one to many relationship, the first item has many relationships to the other item but not the other way around. 
An example of this would be the relation between games and reviews. A game can have many reviews but a review cannot have many games. It would belong to a specific game. For the relationship of review to game, we would use `belongs_to` and for game to review, we would use `has_many`. This relationship is shown in Active Record like this:

``` Ruby
class Game < ActiveRecord::Base
   has_many :reviews
end

class Review < ActiveRecord::Base
   belongs_to :game
end
```
_(**Note**: reviews for `has_many` is plural and game for `belongs_to` is singular because, as described before, a game can have **many** reviews but a review has **one** game.)_

The second and last one we will discuss is the Many to Many association. This one is a little more complicated, as a third model is included as well. In a many to many relationship, both the first item and the second item have many relationships with each other. To help keep track, a third item in introduced, which is a merger between the two. 
An example of this would be the relation between games and users. A game `has_many` users and a user also `has_many` games. To help keep track, we will introduce the third one, which will be reviews, as a merger between the two. 
Now we can say that a game `has_many` reviews, a user `has_many` reviews, but a review `belongs_to` a game and a user. For the relationship between a game and review AND user and review, we will use `has_many`. For the relationship between review and user AND review and user, we will use `belongs_to`. This relationship will be shown like this:

``` Ruby
class Game < ActiveRecord::Base
   has_many :reviews
   has_many :users, through: :reviews
end

class User < ActiveRecord::Base
   has_many :reviews
   has_many :games, through: :reviews
end

class Review < ActiveRecord::Base
   belongs_to :game
   belongs_to :user
end
```
_(**Note**: For the second `has_many` for Game and User, we have added a `through:` because Review is the merger that connects them to each other!)_

In conclusion, there are two types of Active Record Associations that are discussed. The first one is the One to Many association and the second one is the Many to Many association. One to Many is when first item `has_many` of the second item but the second only has one of the first.To show this, a `belongs_to` is used for one and a `has_many` for the other. This shows that the Many to Many is when the first item `has_many` of the second and vice versa. To keep track, there is a third item introduced which `belongs_to` both of the items. To show this relationship, a `has_many: , through:` is used to show the relationship between the three!