diff --git a/app/models/memo.rb b/app/models/memo.rb index aa0af8a02..30fb80490 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -188,7 +188,19 @@ class Memo < ApplicationRecord end def update_attachments(attachment_ids) - Attachment.where(id: attachment_ids).update_all(container_id: id, container_type: "Memo") + if attachment_ids.present? + attachment_ids = attachment_ids -[nil] if attachment_ids.is_a? Array + old_attachment_uuids = Attachment.where(container_id: id, container_type: "Memo").pluck(:uuid) + if old_attachment_uuids.present? + del_attachment_uuids = old_attachment_uuids - attachment_ids + Attachment.where(uuid: del_attachment_uuids).update_all(container_id: nil, container_type: nil) + end + if attachment_ids.present? + Attachment.where(uuid: attachment_ids).update_all(container_id: id, container_type: "Memo") + end + else + Attachment.where(container_id: id, container_type: "Memo").update_all(container_id: nil, container_type: nil) + end end private