Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order mailboxer inbox?

I am using mailboxer gem with my rails application, and i want to order my inbox messages so that when the user receives a new message, I would like to have a notification or keep track of which messages have been read and which have not and order the messages to have the unread / new messages at the top of the page.

Here is my conversations controller

class ConversationsController < ApplicationController
  before_action :get_mailbox
  before_action :get_conversation, except: [:index]

  def index
    @unread_messages = @mailbox.inbox(unread: true).count
    @conversations = @mailbox.inbox({page: params[:page], per_page: 10})
  end

  private

  def get_conversation
    @conversation ||= @mailbox.conversations.find(params[:id])
  end

  def get_mailbox
    @mailbox ||= current_user.mailbox
  end
end

i tried to order the mail by:

@conversations = @mailbox.inbox({page: params[:page], per_page: 10}).joins(:receipts).select("mailboxer_conversations.*, mailboxer_receipts.*").order('mailboxer_receipts.is_read')

but it did not worked.

Please suggest a solution.

like image 372
Sachin Singh Avatar asked Jul 07 '15 04:07

Sachin Singh


1 Answers

try this,

def index
  @unread_messages = @mailbox.inbox(is_read:false).count
  @conversations = @mailbox.inbox.page(params[:page]).per(10)
end

and by default the inbox is ordered to show the newest message on the top, for example when you type @mailbox.inbox.first you are actually pulling the last (newest) message.

like image 115
Mohamed Abouelsaadat Avatar answered Nov 03 '22 21:11

Mohamed Abouelsaadat