Column | Type | Options |
---|---|---|
name | string | null: false |
string | null: false | |
password | string | null: false |
Column | Type | Options |
---|---|---|
name | string | null: false |
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.
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.
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.