Messaging with Rails and Mailboxer

Great tutorial, thank you for writing. Though I ran into two issues.

  1. I get an error when sending messages: wrong number of arguments (1 for 0)
    It points to this line in the messages_controller:
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation

Here is a stack trace:

Started POST "/messages" for 10.0.2.2 at 2015-10-10 16:23:39 +0000
Processing by MessagesController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"YiqT94X0gvp2W9aT1PNTx6iRM3E40ozMlkgZsU6LGpfoBxTygHFdBSkeyUYHHwGwWX+x8QpjJTEj4HYyrqhxng==", "message"=>{"subject"=>"work", "body"=>"pleas work this time!"}, "recipients"=>["2"], "commit"=>"Send"}
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2
(0.2ms)  BEGIN
SQL (0.7ms)  INSERT INTO "mailboxer_conversations" ("subject", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["subject", "work"], ["created_at", "2015-10-10 16:23:39.424583"], ["updated_at", "2015-10-10 16:23:39.424583"]]
SQL (0.3ms)  INSERT INTO "mailboxer_notifications" ("type", "sender_id", "sender_type", "body", "subject", "created_at", "updated_at", "conversation_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["type", "Mailboxer::Message"], ["sender_id", 1], ["sender_type", "User"], ["body", "pleas work this time!"], ["subject", "work"], ["created_at", "2015-10-10 16:23:39.424583"], ["updated_at", "2015-10-10 16:23:39.424583"], ["conversation_id", 10]]
SQL (0.2ms)  INSERT INTO "mailboxer_receipts" ("mailbox_type", "receiver_id", "receiver_type", "notification_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["mailbox_type", "inbox"], ["receiver_id", 2], ["receiver_type", "User"], ["notification_id", 12], ["created_at", "2015-10-10 16:23:39.436761"], ["updated_at", "2015-10-10 16:23:39.436761"]]
(0.8ms)  COMMIT
(0.3ms)  BEGIN
SQL (0.2ms)  INSERT INTO "mailboxer_receipts" ("mailbox_type", "receiver_id", "receiver_type", "is_read", "notification_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["mailbox_type", "sentbox"], ["receiver_id", 1], ["receiver_type", "User"], ["is_read", "t"], ["notification_id", 12], ["created_at", "2015-10-10 16:23:39.441001"], ["updated_at", "2015-10-10 16:23:39.441001"]]
(0.5ms)  COMMIT
(0.2ms)  SELECT COUNT(*) FROM "mailboxer_conversation_opt_outs" WHERE "mailboxer_conversation_opt_outs"."conversation_id" = $1 AND "mailboxer_conversation_opt_outs"."unsubscriber_type" = $2 AND "mailboxer_conversation_opt_outs"."unsubscriber_id" = $3  [["conversation_id", 10], ["unsubscriber_type", "User"], ["unsubscriber_id", 2]]
Completed 500 Internal Server Error in 23ms (ActiveRecord: 3.8ms)

ArgumentError (wrong number of arguments (1 for 0)):
app/controllers/messages_controller.rb:9:in `create'

Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.5ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (23.1ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (12.8ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (27.3ms)
  1. I am getting the “Resource Not Found” message when I try to empty the trash.

    Started DELETE “/conversations/empty_trash” for 10.0.2.2 at 2015-10-10 16:21:49 +0000
    Processing by ConversationsController#empty_trash as HTML
    Parameters: {“authenticity_token”=>“mhCTp4FfbBlr573VGWT8PKM16Lu5pAKo93nHVfNMF075wWvnIUMkpO+ipat7rsP3dLNWf7WyYmXxphcQK6C+0w==”}
    User Load (0.3ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 ORDER BY “users”.“id” ASC LIMIT 1 [[“id”, 2]]
    Redirected to http://localhost:3000/conversations?box=trash
    Completed 302 Found in 4ms (ActiveRecord: 0.3ms)

    Started GET “/conversations?box=trash” for 10.0.2.2 at 2015-10-10 16:21:49 +0000
    Processing by ConversationsController#index as HTML
    Parameters: {“box”=>“trash”}
    User Load (0.3ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 ORDER BY “users”.“id” ASC LIMIT 1 [[“id”, 2]]
    (0.4ms) SELECT DISTINCT COUNT(DISTINCT “mailboxer_conversations”.“id”) FROM “mailboxer_conversations” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“conversation_id” = “mailboxer_conversations”.“id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) INNER JOIN “mailboxer_receipts” ON “mailboxer_receipts”.“notification_id” = “mailboxer_notifications”.“id” WHERE “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“mailbox_type” = $1 AND “mailboxer_receipts”.“trashed” = $2 AND “mailboxer_receipts”.“deleted” = $3 AND “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“receiver_id” = $4 AND “mailboxer_receipts”.“receiver_type” = $5 AND “mailboxer_receipts”.“is_read” = $6 [[“mailbox_type”, “inbox”], [“trashed”, “f”], [“deleted”, “f”], [“receiver_id”, 2], [“receiver_type”, “User”], [“is_read”, “f”]]
    (0.8ms) SELECT DISTINCT COUNT(DISTINCT “mailboxer_conversations”.“id”) FROM “mailboxer_conversations” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“conversation_id” = “mailboxer_conversations”.“id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) INNER JOIN “mailboxer_receipts” ON “mailboxer_receipts”.“notification_id” = “mailboxer_notifications”.“id” WHERE “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Conversation Load (0.7ms) SELECT DISTINCT “mailboxer_conversations”.* FROM “mailboxer_conversations” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“conversation_id” = “mailboxer_conversations”.“id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) INNER JOIN “mailboxer_receipts” ON “mailboxer_receipts”.“notification_id” = “mailboxer_notifications”.“id” WHERE “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 ORDER BY mailboxer_conversations.updated_at DESC LIMIT 10 OFFSET 0 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    (0.7ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 9 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.4ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 9]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 11]]
    User Load (0.2ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    User Load (0.2ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (6.2ms)
    Mailboxer::Message Load (0.2ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 9]]
    (0.5ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 8 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 8]]
    Mailboxer::Receipt Load (0.1ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 10]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (5.6ms)
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 8]]
    (0.3ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 7 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.2ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 7]]
    Mailboxer::Receipt Load (0.1ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 9]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (5.2ms)
    Mailboxer::Message Load (0.4ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 7]]
    (0.5ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 6 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 6]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 8]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (6.1ms)
    Mailboxer::Message Load (0.5ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 6]]
    (0.4ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 4 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 4]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 4]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    Rendered conversations/_participants.html.erb (5.2ms)
    Mailboxer::Message Load (0.2ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 4]]
    (0.5ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 2 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 2]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (6.2ms)
    Mailboxer::Message Load (0.6ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 2]]
    (0.7ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 1 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 1]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 1]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (5.7ms)
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 1]]
    Rendered conversations/_conversation.html.erb (163.7ms)
    Rendered conversations/index.html.erb within layouts/application (186.6ms)
    Rendered layouts/_nav.html.erb (3.0ms)
    Rendered layouts/_alert.html.erb (0.7ms)
    Completed 200 OK in 637ms (Views: 621.1ms | ActiveRecord: 12.2ms)

If you need any more information, please let me know. I have been staring at this code for days now trying to figure out what’s wrong.

Thanks

I’ll look into it!

Do you have this project on GitHub?

Here it is: https://github.com/angelamancini/mailboxer-test

Well, unfortunately I wasn’t able to boot the app, because it contains too many specific configuration that I have to alter. If you could make this more straighforward (like using sqlite3), I’d try to play with it a bit more. All in all, I don’t really understand what could be the cause, as I my demo app is working correctly with new mailboxer. What I would recommend is just compare your code with mine to see any differences.

Thanks for giving it a shot. I did fix the empty trash issue. I forgot to add :empty_trash to this line in the conversations_controller.rb

 before_action :get_conversation, except: [:index, :empty_trash]

I will continue to compare your code with mine and post the answer, just in case someone else runs into this.

Thank you for this awesome tutorial!

This is probably easy but I’m totally lost.
Basically I need to let a user send a message to multiple users but keep conversations ‘filtered’ in a way that recipients can only see the messages from the originator and the ones they own.

Does it make sense? Is this suitable for this?

UPDATE
I’ve edited the show view like this:

  <div class="panel-body">
    <% @conversation.messages.each do |message| %>
      <% if message.sender == @conversation.originator or message.sender == current_user %>
      <div class="messages">
        <div class="media">
          <div class="media-left">
            <%= gravatar_for message.sender, 45, message.sender.full_name %>
          </div>
          <div class="media-body">
            <h6 class="media-heading">
              <%= message.sender.full_name %> (<%= message.created_at.strftime("%-d %B %Y, %H:%M") %>)
            </h6>
            <p><%= message.body %></p>
          </div>
        </div>
      </div>
      <hr>
      <% end %>
    <% end %>
  </div>

Is there something more I should take into account?
Is this the best way to do it?

Well, you might extract those conditions somewhere from the views, but all in all this seems to be okay!

Thank you! Actually this won’t work for what I’m trying to achieve.

Is it possible to split a new message in different conversations depending on the number of recipients chosen?

Is this ok?

  def create
    recipients = User.where(id: params['recipients'])
    recipients.each do |recipient|
    	conversation = current_user.send_message(recipient, params[:message][:body], params[:message][:subject]).conversation
    end
    flash[:success] = "Message has been sent!"
    redirect_to conversations_path
  end

Of course, I haven’t tested, but at the first glance it seems okay, though I’m not sure why would you need this :slight_smile:

I need some way a user can send the same message to multiple users but keep the conversations separated.
I guess this may sound weird and it misses the all idea of conversations.

I was thinking I could make this the default behavior and add a checkbox on new message view to set the message to be a private conversation (splitting it) or grouped.

Does it make sense? Is it possible or am I thinking it the wrong way and should build something from scratch?

I’m not sure what’s wrong, but my issue was fixed by copying the initializer from the demo code into my project. Go figure. Thanks for the tutorial!

Thanks so much for taking the time to write this. It really helped in getting mailboxer setup. I couldn’t find any solid docs on implementing the Message#create method for multiple models, since the recipients array is just id’s (might be possible/easier if looking up by unique usernames). I ended up doing it this way for anyone that needs some direction.

In the recipients_options helper, I updated the value to include the class (User, Duck, Cylon, etc.):

<option value='#{user.class.name}:#{user.user_id}'>#{user.name}</option>

Then in the Messages controller, I just had to break it apart to figure out what type of user it is:

def create
    recipients = []
    params[:recipients].each do |r|
      if r.split(':').first === 'User'
        recipient = User.find(r.split(':').last)
        recipients << recipient
      elsif r.split(':').first === 'Duck'
        recipient = Duck.find(r.split(':').last)
        recipients << recipient
      elsif r.split(':').first === 'Cylon'
        recipient = Cylon.find(r.split(':').last)
        recipients << recipient
      end
    end
    conversation = @user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversation_path(conversation)
  end

@bodrovis - Would love to hear your thoughts on this execution.

I’ll try to look into it.

I believe you can stick with eval here.

recipient = eval("#{r.split(':').first}.find(r.split(':').last)"

Just add some error checkings and whitelist models that can be referenced.

@bodrovis That certainly looks cleaner. I’ll take a look at implementing. Thanks again for the update and for writing this great resource.

Hi there

Is this possible to create more than two conversations between two same users?
When I try to do this, it throws:

Validation failed: Notification base This message must be added to existing conversation (#)

Hi! That depends on what code are you using. Generally, yes it should be possible.

@bodrovis, I am calling

user.send_message(recipient, 'body', 'subject')

each time for same user and recipient. So, for each message, should be created a new conversation.

Can you please tell me how can I do this?