To put it simply, it means "associate each model with each other." In other words, the tables are associated with each other so that ** one model can access the other **.
Example. ** A pattern in which one user owns multiple comments (one-to-many relationship) ** → Connect "** A-san " with A-san's comments " Good morning " and " Good evening **". → is "Mr. ** B " and B's comment " Hello ", linking the " goodbye **".
Now let's show how to reproduce the association in code. Consider the above ** pattern in which one user owns multiple comments (one-to-many relationship) **. (1) has_many
#User model
class User < ApplicationRecord
has_many :comments #Since one user owns multiple comments, it becomes a plural comment.
end
(2) belongs_to
#Comment model
class Comment < ApplicationRecord
belongs_to :user #One comment is owned by one user, so it is a singular user.
end
I explained the basic idea of one-to-many associations. In the future, I would like to continue to post a little more applied things such as many-to-many relationships. Also,. If you have any mistakes, please comment!