[RUBY] [Rails] Many-to-many creation

Purpose of this post

・ Minutes about Rails.

Teaching materials used for learning

I have summarized it with reference to the following two teaching materials of Udemy. ・ ["Introduction to Ruby on Rails for the first time-Learn Ruby and Rails from the basics and publish your web application on the net"](https://www.udemy.com/course/the-ultimate-ruby-on-rails-bootcamp/) ・ ["Rails engineer training course taught by full stack engineers"](https://www.udemy.com/course/rails-kj/)

○ What is the many-to-many relationship?

・ Relationship between models in which multiple columns are linked to each other ・ Example): The relationship between the board of the bulletin board application and the tag (posts with different posts but similar tags have a many-to-many relationship)

○ Points of linking

・ Prepare an intermediate table (defined as model (middle) in the explanation in this post) -The intermediate table has a simple structure with only [: id, many 1_id, many 2_id]

○ Linking setting method

① Generate model many 1 and model many 2 normally

rails g <model(Many)1> <column information>
rails g <model(Many)2> <column information> 

② Generate intermediate table

rails g model <model(During ~)> <model(Many)1>:references <model(Many)2>:references

-Intermediate tables are usually named "model (many) 1_model (many) 2_relations" ・ Model names are all lowercase and singular.

③ Execute migration

rails db:migrate

・ The model set in ①② is reflected in the table of db

④ Description of intermediate table in model

model(During ~).rb


class model(During ~) < ApplicationRecord
  belongs_to :model(Many)1
  belongs_to :model(Many)2
end

・ This description is automatically written at the time of generate ・ Since two belongs_to have already been set, no touch is required.

⑤ processing of moel (many) 1 into model

model(Many)1.rb


class model(Many)1 < ApplicationRecord
has_many :<model(During ~)s>
has_many :<model(Many)2s>, through: :<model(During ~)s>
end

Through :: <model (middle) s> means that it goes through model (middle) between two models. ・ (Meaning model (many) 1 → model (medium) → model (many) 2) ・ It is necessary to describe here that it goes through the intermediate table. ・ Model name is described in plural form

⑥ Processing of model (many) 2 to model

model(Many)2.rb


class model(Many)2 < ApplicationRecord
has_many :<model(During ~)s>
has_many :<model(Many)1s>, through: :<model(During ~)s>
end

・ Set two has_many as in model (many) 1. ・ Model name is described in plural form


Recommended Posts

[Rails] Many-to-many creation
Portfolio creation Ruby on Rails
API creation with Rails + GraphQL
Rails reference type creation added
[Rails] New app creation --Notes--
Add & save from rails database creation
[Rails] Initial data creation with seed
[Ruby on Rails] Confirmation page creation
[Rails] Implementation of many-to-many category functions
[Rails] Types of associations (one-to-many / many-to-many)
[Rails g.error]
Association (many-to-many)! !!
Rails basics
Rails Review 1
[Rails] first_or_initialize
rails tutorial
About Rails 6
Ruby on Rails model creation / deletion command
[Rails] views file creation # text is displayed
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
Ruby on Rails application new creation command
(2021) Ruby on Rails administrator (admin) login creation
[Rails] devise
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
[Beginner Procedure Manual 2] Ruby on Rails: Rails template creation