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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With