Search code examples
ruby-on-railsruby-on-rails-4mailboxer

Mailboxer + Rails 4.1: Message to myself shows duplicate sentbox and inbox message in conversation


Using mailboxer gem version 0.12.2 with Rails version 4.1.4.

When I send a message to myself, the Conversation view shows the same message twice (once as a mailbox_type=sentbox and once as an mailbox_type=inbox). I do not have this issue with messages sent to other users.

I simply just want to show the message once in my conversation view (ideally the inbox version when viewing my inbox and the sentbox version when viewing my sent messages.) Is there a way I can do that?

Here's my show action in my custom ConversationsController (current_user is a devise method indicating the currently signed in user)

def show
  @mailbox = current_user.mailbox
  @conversation = @mailbox.conversations.find(params[:id])
  @receipts = @conversation.receipts_for(current_user)
end

I've also tried the following combinations within my show action, all with the same results.

  • Using the inbox method instead of the conversations method:
    @conversation = @mailbox.inbox.find(params[:id])
  • Using receipts_for method on the @mailbox instance variable:
    @receipts = @mailbox.receipts_for(@conversation)

Here's my corresponding show.html.erb view

<ul>
  <%= content_tag_for(:li, @receipts) do |receipt| %>
    <% message = receipt.message %>
    <strong>From:</strong> <%= message.sender.email %>
    <br/>
    <strong>Message:</strong><%= simple_format h message.body %>
    <strong>Sent:</strong> <%= @conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %>

    <div>
      <br/>DEBUG INFO:<br/>
      Message Id <%= message.id %><br/>
      Receipt Id <%= receipt.id %><br/>
      Receipt Mailbox type <%= receipt.mailbox_type %><br/>
    </div>
    <hr/>

  <% end %>
</ul>

Is there anyway to just get my "inbox" view for conversations with myself not to show duplicate messages?


Solution

  • Thanks to searsaw for answering this question on the gem's github page:

    def show
      @mailbox = current_user.mailbox
      @conversation = Mailboxer::Conversation.find(params[:id])
      @receipts = @conversation.receipts_for(current_user).inbox
    end
    

    The line that did it is @conversation.receipts_for(current_user).inbox. Calling inbox on the receipts "gets the receipts for a logged in user from that conversation that are labeled with the mailbox_type of inbox."

    Limitations

    Unfortunately, the contents of the @receipts variable for the Conversation's show view might not be desirable for conversations with other users if you use the inbox method on receipts - the current_user's sent messages will not be visible in the Conversation's show view with other users. This is a limitation of the mailboxer gem (Mailboxer was not designed with being able to send messages only to yourself).

    In the end, extra logic in the controller/view will be needed if you need to support sending messages to only yourself.