[英]Rails - Mailboxer : delete from database if both participant deleted conversation
I am trying to delete a conversation with MailBoxer gem. 我想删除与MailBoxer gem的对话。 There's nothing included in the gem itself, but I think it's possible to do something like:
宝石本身并没有包含任何内容,但我认为可以做以下事情:
If both participants deleted the conversation, delete it from the database. 如果两个参与者都删除了对话,请将其从数据库中删除。
I started with something like this in my function "empty_trash", so like that, every time a participant click on the link "Empty Trash", it will check if the conversation has been deleted by both or not. 我在我的函数“empty_trash”中开始使用类似的东西,所以每当参与者点击“清空垃圾箱”链接时,它都会检查对话是否已被两者删除。 If not, nothing happens, if yes, it will delete the conversation from the DB.
如果没有,没有任何反应,如果是,它将从数据库中删除对话。
def empty_trash
@conversations = current_user.mailbox.conversations
@conversations.each do |conversation|
conversation.receipts_for(current_user).update_all(deleted: true)
end
@delete_conversation = Mailboxer::Receipt.select(:notification_id,:deleted).
group(:notification_id,:deleted).
having("count(*) > 1")
@delete_conversation.destroy_all
redirect_to :back
end
So basically, what I am trying to do, is to group all conversation by :notification_id where deleted: true and then delete the matching result. 所以基本上,我要做的是将所有会话分组:notification_id where deleted:true然后删除匹配结果。
EDIT: DB 编辑:DB
t.string "receiver_type"
t.integer "receiver_id"
t.integer "notification_id", null: false
t.boolean "is_read", default: false
t.boolean "trashed", default: false
t.boolean "deleted", default: false
t.string "mailbox_type", limit: 25
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "is_delivered", default: false
t.string "delivery_method"
t.string "message_id"
ID | NOTIFICATION_ID| DELETED |
=================================
1 | 9 | t |
2 | 9 | t |
3 | 8 | f |
4 | 8 | t |
So in this example, the records with "Notification_id" 9, should be destroyed. 因此,在此示例中,应销毁带有“Notification_id”9的记录。
EDIT 2 - After you updated your answer : I ended up with this, which is working, I just don't know if it's the cleaner approach, but at least it work. 编辑2 - 在您更新答案之后:我最终得到了这个,这是有效的,我只是不知道它是否是更清洁的方法,但至少它是有效的。 I think later I will move to a home made messaging system, certainly more flexible.
我想以后我会转向自制邮件系统,当然更灵活。
def empty_trash
@conversations = current_user.mailbox.conversations
@conversations.each do |conversation|
conversation.receipts_for(current_user).update_all(deleted: true)
end
@a = Mailboxer::Receipt.where(receiver_id: current_user.id)
@b = Mailboxer::Receipt.where.not(receiver_id: current_user.id)
@a.each do |a|
@b.each do |b|
if a.notification_id == b.notification_id && a.deleted == true && b.deleted == true
b.delete
a.delete
end
end
end
redirect_to :back
end
Updated answer 更新的答案
@delete_conversation = []
@conversations = Conversation.mailbox.conversations
@conversations.each do |conversation|
@conversations.each do |compareConv|
if conversation.notification_id == compareConv.notification_id && conversation.deleted? && compareConv.deleted && conversation.receiver_id != compareConv.receiver_id
@delete_conversation.push(conversation)
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.