A validation error occurred when saving to the intermediate table.

I got a validation error when trying to process an array with ids in an intermediate table

user table

Column Type Options
name string null: false
email string null: false
password string null: false

room table

Column Type Options
name string null: false

room_user table

Column Type Options
user references null: false, foreign_key: true
room references null: false, foreign_key: true
#description of view
<select name="room[user_ids][]">
  <option value="">Please select a user to chat with</option>
  <% User.where.not(id: current_user.id).each do |user| %>
    <option value=<%=user.id%>><%= user.name %></option>
  <% end %>
</select>
<input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
#Description of controller

  def create
    @room = Room.new(room_params)
    if @room.save
      redirect_to root_path
    else
      render :new
    end
  end

  private

  def room_params
    params.require(:room).permit(:name, user_ids: [])
  end
end

The description so far works fine, so I didn't change it.

Suddenly it became impossible to save user_id in the intermediate table.

I don't know the cause, but when I was doing something completely unrelated, a validation error suddenly occurred, and I couldn't save the value and the Room model.

The solution

I don't know the reason, so it might be a bit brute force, but this was the only way to solve it, so it worked fine.

  has_many :room_users
  has_many :users, through: :room_users, validate: false
# validate:false postscript

By writing without validation, I was able to pass the value as an array to the intermediate table and save it well.

Recommended Posts

A validation error occurred when saving to the intermediate table.
[Ruby] When adding a null constraint to a table
[Grails] Error occurred running What to do when the Grails CLI does not start
How to make a unique combination of data in the rails intermediate table
An error occurred when executing a function from MyBatis with the OUT parameter set to CURSOR in PostgreSQL.
What to do when a null byte error occurs
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
[Rails] Processing after adding a column to the devise table
Solution for the error "no basic auth credentials" when pushing a Docker container to Heroku
[Java] Adding an element to the Collection causes a compile error
I get a Ruby version error when I try to start Rails.
I tried to translate the error message when executing Eclipse (Java)
Easy way to create a mapping class when using the API
How to solve the unknown error when using slf4j in Java
How to add columns to a table
About error when implementing spring validation
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
How to set the default fetch size when jOOQ gets an OOM error when fetching a huge result set
[Rails] I learned about migration files! (Adding a column to the table)
A memo when you want to clear the time part of the calendar
[IntelliJ IDEA] How to automatically add final when saving a Java file
[Unauthorized Operation] A memorandum because an error occurred when creating an EC2 instance.
A story I was addicted to when testing the API using MockMVC
[Rails] About the error when displaying the screen due to the autofocus of the form
How to display error messages and success messages when registering as a user
[Java small story] Monitor when a value is added to the List
I want to display an error message when registering in the database
[Rails] How to create a table, add a column, and change the column type
How to reference a column when overriding the column name method in ActiveRecord
What to do if you get an "A server is already running." Error when you try to start the rails server
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase
How to put out the error bundling
The road to creating a music game 2
Error when trying to use heroku command
Error in implementation when implementing Spring validation
A reminder when an aapt.exe error occurs
The road to creating a music game 3
The road to creating a music game 1
What I tried when I wanted to get all the fields of a bean
[Rails] How to register multiple records in the intermediate table with many-to-many association
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
When I try to run docker-compose, I get the error "Cannot locate specified Dockerfile: Dockerfile"
[IntelliJ IDEA] How to format only local changes when saving the source code
[Rails] When transitioning to a page with link_to, move to the specified location on the page
I managed to get a blank when I brought the contents of Beans to the textarea
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error