[RUBY] [Rails] How to register multiple records in the intermediate table with many-to-many association

Features you want to implement

In a many-to-many model relationship, I want to register multiple selected values in the intermediate table with the check box. I was really into it when I implemented it, so I'll leave it as a memorandum.

environment

Rails 5.2 Ruby 2.5.1 MacOSX

Model relationship

--Project model

class Project < ApplicationRecord
  has_many :project_phases, dependent: :destroy
  has_many :mst_phases, through: :project_phases
end

--Project process

class ProjectPhase < ApplicationRecord
  belongs_to :project
  belongs_to :mst_phase
end

--Process master

class MstPhase < ApplicationRecord
  has_many :project_phases
end

Regarding the process, only the project_phase model is associated in the master table so that the project model side is not referenced.

View

<%= form_with model: @project, local: true do |f| %>
  <label>Project name</label>
  <%= f.text_field :project_name %>

  <label>Project process</label>
  <%= f.collection_check_boxes :mst_phase_ids, MstPhase.all, :id, :phase %>
<% end %>

Controller

class ProjectsController < ApplicationController

  def new
    @project = Project.new
    @project.project_phases.build
  end

  def create
    @project = Project.new(projects_params)
    
    if @project.save
      redirect_to project_path @project
    else
      render action: 'new'
    end
  end

  private
  def projects_params
    params.require(:project).permit( :project_name, mst_phase_ids: [])
  end
end

The point is to set StrongParameter to receive models related to has_many as an array.

The names of the parameters should be described as follows.

Model name_ids

Since it is not necessary to implement any special logic for the parameters received in the array, records are registered in the table for the number of elements in the array. Rails convenient. .. .. .. ..

Recommended Posts

[Rails] How to register multiple records in the intermediate table with many-to-many association
How to make a unique combination of data in the rails intermediate table
[Rails] How to get the user information currently logged in with devise
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
How to check Rails commands in the terminal
[Rails] How to operate the helper method used in the main application with Administrate
How to change the action with multiple submit buttons
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
[Rails] How to change the column name of the table
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to query Array in jsonb with Rails + postgres
[Rails] How to display an image in the view
[With back tricks] How to introduce React to the simplest Rails
[Rails] How to display information stored in the database in view
I want to fetch another association of the parent model from the intermediate table with has_many
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
How to get along with Rails
How to install Swiper in Rails
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to introduce kaminari with Slim and change the design
How to get boolean value with jQuery in rails simple form
How to rename a model with foreign key constraints in Rails
[Rails] How to reset the database in production environment (Capistrano version)
How to call multiple names at once in the same category
How to write the view when Vue is introduced in Rails?
How to implement search functionality in Rails
How to implement search function with rails (multiple columns are also supported)
How to change app name in rails
How to decorate the radio button of rails6 form_with (helper) with CSS
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to use custom helpers in rails
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] How to log in with a name by adding a devise name column
[Rails] How to use rails console with docker
[Rails] How to use the map method
[JPA] How to save when there are items other than keys in the intersection table related to ManyToMany
How to use MySQL in Rails tutorial
[Rails 6] Register and log in with Devise + SNS authentication (multiple links allowed)
[rails] How to configure routing in resources
How to get the date in java
How to compare only the time with Rails (from what time to what time, something like)
How to implement ranking functionality in Rails
[Rails] How to create a table, add a column, and change the column type
How to debug the processing in the Ruby on Rails model only on the console
How to use credentials.yml.enc introduced in Rails 5.2
How to execute multiple commands in docker-compose.yml
How to build Rails 6 environment with Docker
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
[Rails] How to read the XML file uploaded from the screen with Hash type
How to get the ID of a user authenticated with Firebase in Swift
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to resolve errors that occur in the "Ruby on Rails" integration test
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
[Rails] How to decide the destination by "rails routes"
How to check the logs in the Docker container
Assignment to multiple variables with the ternary operator