How to create and update related models ?

25 views
Skip to first unread message

Николай Спелый

unread,
Sep 10, 2015, 7:14:13 AM9/10/15
to Ruby on Rails: Talk
Hellow.
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|
    chatusers.build(user_id: chat_user, num_unread_msgs: 0)
  end
end

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

Thanks.
Reply all
Reply to author
Forward
0 new messages