Home>

In Ruby on Rails, like the group function of LINE, the function to enter the group name and select the member from the dropdown to create a group is implemented.
Group_users is set in the User model, Group model, and intermediate table.
I want to implement so that the group name, user_id, and group_id created in the group model and group_users table can be saved, but I am stuck with an error.

When I tried to save the data on the console, the following error message was output.

[1] pry (main)>@group = Group.new (name: "group1", user_ids: [8, 9])
=>#<Group: 0x00007fdec218cf38
 id: nil,
 name: "group1",
 created_at: nil,
 updated_at: nil>
[2] pry (main)>@ group.save!
ActiveRecord :: RecordInvalid: Validation failed: Users is invalid
from /Users/〇〇/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/validations.rb:80:in `raise_validation_error'
Corresponding source code User model
class User
Group model
class Group
group_users table
class GroupUser
What I tried

Although it was a validation error, I tried deleting the validation of the Group model because the validation setting was only name for this implementation, but it did not change.
Other than that, I did not find any problem with validation, so when I investigated the model, I found out that there is an option called optional in belongs_to set in the intermediate table, and I wrote it. However, even so, the output result did not change and a validation error occurred, so I was suffocated.
Since I am a beginner, it will be basic, but I would appreciate your help. There may be some deficiencies, but thanks for your understanding.

Supplementary information (FW/tool version, etc.) Development environment

Ruby: 2.6.5
Ruby on Rails: 6.0.0
DB: Mysql 5.6.47