ruby - Rails - Mailboxer : delete from database if both participant deleted conversation -


i trying delete conversation mailboxer gem. there's nothing included in gem itself, think it's possible like:

if both participants deleted conversation, delete database.

i started in function "empty_trash", that, every time participant click on link "empty trash", check if conversation has been deleted both or not. if not, nothing happens, if yes, delete conversation db.

def empty_trash     @conversations = current_user.mailbox.conversations         @conversations.each |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, trying do, group conversation :notification_id deleted: true , delete matching result.

edit: 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 example, records "notification_id" 9, should destroyed.

edit 2 - after updated answer : ended this, working, don't know if it's cleaner approach, @ least work. think later move home made messaging system, more flexible.

def empty_trash     @conversations = current_user.mailbox.conversations         @conversations.each |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 |a|      @b.each |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 |conversation|  @conversations.each |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 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -