How to create and update related models ?

I need to create and update related models i.e. Chat—Chatusers—User.

I have already each-creating in model Chat

def set_members

@chat_users.each do |chat_user| chat_user, num_unread_msgs: 0)



At first i think this is not a nice solution.

At second i need to update already existing records and if some of them are not represented in input array they must be deleted.

What i looking for may looks like:

(in controller i.e.)

@chat = Chat.find_by(id: params[:id])

@chat.chatusers.update(user_id: params[:chatusers_ids])

I have list of ids, but it’s not to difficult to get list of objects Chatuser

at first question create need to be instead of update