Rails - Mailboxer:如果两个参与者都删除了对话,则从数据库中删除

时间:2017-04-07 16:10:44

标签: ruby-on-rails ruby rubygems mailboxer

我正在尝试删除与MailBoxer gem的对话。宝石本身没有任何内容,但我认为可以做以下事情:

如果两个参与者都删除了对话,请将其从数据库中删除。

我在我的函数" empty_trash"中开始使用类似的东西,所以每次参与者点击链接"清空垃圾箱"时,它会检查对话是否有已被两者删除。如果没有,没有任何反应,如果是,它将从数据库中删除对话。

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

所以基本上,我要做的是将所有会话分组:notification_id where deleted:true然后删除匹配结果。

编辑: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 |

所以在这个例子中,带有" Notification_id"的记录9,应该被销毁。

编辑2 - 更新答案后: 我最终得到了这个,这是有效的,我只是不知道它是否是更清洁的方法,但至少它是有效的。 我想以后我会转向自制的消息传递系统,当然会更灵活。

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

1 个答案:

答案 0 :(得分:0)

更新回答

<(.*)>
相关问题