Association (many-to-many)! !!

What is an association? ?? : information_desk_person_tone2:

** Simply put, it's an association between tables using a model. Associate the tables with each other and change from one model to the other It's to make it accessible. ** **

* This time, I will explain the "many-to-many" association. </ font>

Reasons for associating

*** In Rails, there are two "associations" It's the connection between Active Record models. *** ***

You need to make an association between the two models, Do you know the reason?

*** That's right, by associating, the common operation of the code becomes more It's simple and easy to do. *** ***

Example: Association (many-to-many)

This time, I would like to explain with the users table and the rooms table.

First, let's consider the relationship between each table and other tables.

** · Manage which chat rooms users belong to ** ** · Manage which users are in the chat room **

It will be the following image this time.

多対多.png

The point to note here is the relationship between the users table and the rooms table. In this DB design, there is a "many-to-many" relationship. ** "Many-to-many" is a relationship where each other has multiple ids for related tables. ** </ font>

In this case, "one user belongs to multiple chat rooms" It's a relationship when "multiple users belong to one chat room".

But hey, to show the relationship of this table, I put multiple values on the foreign key It would be easy if you could save it, but you can't save multiple values for a column.

A common practice is to add more columns as the number of associated ids increases. There is a pattern. As an example, I'll put the image below for easy understanding.

無駄なカラム.png

As you can see from the image, I'm worried about the column. There is a useless column.

In such a DB design, the more related records, the more columns, so It's a bad design, isn't it?

The ** intermediate table ** is used to solve this.

Intermediate table

Now, what do you think of an intermediate table, so I will explain it briefly. *** An intermediate table, as the name implies, is a table between two tables. *** ***

In this case, we will create an intermediate table between the users table and the rooms table.

*** The intermediate table is sandwiched between two tables in a "many-to-many" relationship, and only the two combination patterns are saved as records. *** ***

** Also, only two models have a "many-to-many" association It cannot be assembled. Therefore, we use an intermediate table to define a "many-to-many" relationship. ** **

中間テーブル.png

I may not have a good image with this explanation yet, so Next, I will explain using SNS as an example. The table name will be changed from the following explanation, so it is different from the previous table name. I'm sorry if it's hard to understand. .. </ font>

Let's check the role of the intermediate table using the tagged photo posting app as an example

Imagine a photo posting app that allows you to tag a single photo multiple times, such as Instagram. There are three tables to realize the tagging function like "Instagram" Is required. Table to store tags, table to store photos, And it is a table that saves "which tag is registered in which photo".

Of these, the names of the tables that store photos and tags should be the tags table and the photos table. The table of "Which tag is ~ in which photo" is the photos_tags table. The name of the two related tables connected by an underscore.

The relationships between the records stored in each table are as follows.

instagram.png

** As you can see from the image above, the intermediate table contains information about "which photo is associated with the tag". A combination of "photo_id x tag_id" is recorded in one record, and records are accumulated for the number of all photo and tag combinations. For example, if 10 photos each have 3 different tags, 30 records will be generated in the intermediate table to represent these relationships. ** **

So far, we have explained how to save information in the DB using an intermediate table. From here, we will see how to set up associations using intermediate tables.

In order to make an association to a "many-to-many" table via an intermediate table, it is necessary to describe the through option in the has_many method that has been used so far. I will explain the through option next.

through option

** The through option of the has_many method is used when defining a many-to-many relationship to the model. ** ** As the name ** through means, it means "via". ** ** I will post a reference image in a more understandable way.

参考.png

When using the through option to define a many-to-many association, write the following for each model.

models/photo.rb

class Photo < ApplicationRecord
  has_many :photos_tags
  has_many :tags, through: :photos_tags
end

models/tag.rb

class Tag < ApplicationRecord
  has_many :photos_tags
  has_many :photos, through: :photos_tags
end

models/photos_tag.rb

class PhotosTag < ApplicationRecord
  belongs_to :photo
  belongs_to :tag
end

In the model of two tables in a many-to-many relationship, the has_many method defines "one-to-many" associations with each other. Specifies the intermediate table to go through with the through option.

On the other hand, in the intermediate table model, the belongs_to method specifies two tables in a many-to-many relationship.

That's how to define a "many-to-many" association.

Summary

**-Use an intermediate table for "many-to-many" associations · Has_many method in a model of two tables in a "many-to-many" relationship Together with defining "one-to-many" associations with each other, Use the through option to specify the intermediate table to go through **

If this explanation is too long and difficult to understand, Excuse me. .. I've done my best to put it together, so I hope it's helpful. </ font>

that's all.