[RUBY] Association (one-to-many)! !!

What is an association? ?? : information_desk_person_tone1:

** 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, we will explain the "one-to-many" association. There are also "many-to-many" and "one-to-one" associations, so that is I will introduce it at another time.

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, more common operations of code It's simple and easy to do. ** **

I don't think you can understand it even if it is explained in words, so make it easier to imagine. Below is an image that says that the association is like this.

Example: Association (one-to-many)

アソシエーション.png

The above example is based on the image of SNS etc. that you are often using. Isn't it easy to understand if you get it? If "Mr. A" is yourself, "Post 1" and "Post 4" are the ones you posted. That will be the case. I think this makes it easier to imagine! ?? Lol

Next, I will explain how to actually write it.

Method used when defining an association

I will introduce the method used when writing.

has_many method

** From the perspective of the User model, there are multiple posts created by a user. That is, a user owns multiple posts. ** **

If you look at the image above, "Mr. A" will post "Post 1" and "Post 4". It means that you own more than one.

** This state is called has many relationship, In this case, it can be said that it is in the state of "User has many Tweets". ** **

***   「User has many Tweets」 (Mr. A has many posts 1 and 4) ***

To make this association, the ** has_many </ font> ** method shows that there is a "one-to-many" connection between user and other models.

You can easily understand what is being explained by looking at the image below.

has many.png

belongs_to method

** One post is posted by one user. In other words, a post always belongs to one user because multiple people cannot post one post. ** **

In the image posted above, "Post 1" was posted by "Mr. A". In other words, you cannot post the same post as "Post 1" such as "Mr. B". "Post 1" means that you belong to "Mr. A".

** This state is called the belongs to relationship, and in this case it can be said to be the "Tweet belongs to User" state. ** **

               「Tweet belongs to User」

(Post 1 belongs to Mr. A)

The ** belongs_to </ font> ** method shows that there is a "one-to-one" connection between the Tweet model and another model (User).

belongs_to.png

This time, we have all the methods you need to write. Let's actually write it! !!

How to define an association

Please refer to the explanations so far, and then what you have imagined Just write it down! In this example, I would like to use the User model and Tweet model as the model name.

First, I will give an example from the model of User (Mr. A).

models/user.rb

class User < ApplicationRecord
  
  has_many :tweets
end

The way to write it is has_many: model name (plural). The reason why it becomes plural is that User (Mr. A) owns it. This is because there are multiple posts (posts 1 and 4).

That's all for the User model.

Finally, I'll give you an example of a Tweet model.

models/tweet.rb

 
  belongs_to :user
end

The way to write it is belong_to: model name (singular). One post is owned by one User (Mr. A) It will be in the singular form.

You now have an association between the Tweet model and the User model.

Summary

** 1. Association is the association between tables using a model **

** 2.belongs_to method is a "one-to-one" association between a model and another model. In the method used in case, it becomes a dependent association **

** 3. The has_many method is a "one-to-many" association between a model and another model. In the method used in the case, it should be related to own **

*** This time, I explained about the "one-to-many" association. Also, there are "one-to-one" and "many-to-many" associations, so that is also I would like to explain it at another time. It may be confusing, but the "one-to-many" association is I hope you can understand what it looks like. *** ***

Thank you very much!

Recommended Posts