My problem is that when users reply to each other, it's hard examine the database via SQL and differentiate between user types (so admin, and individual users). The default is just "User", as a string.
When examining the mailboxer code, I know it calls the "reply" method in the Conversations controller. This is the relevant part of the code:
def reply
current_user.reply_to_conversation(conversation, message_params[:body])
flash[:notice] = "Message was sent."
redirect_to conversation_path(conversation)
end
From this, I wanted to do something like:
conversation.messages.sender_type = params[:custom].present? ? params[:custom] : "Admin"
But this does not work, as it says "unknown sender type method". I listed it that way because, based on the mail boxer gem code, messages belongs_to conversation, and the messages model is linked to the table and column value I want to change.
This is the relevant code: https://github.com/mailboxer/mailboxer/blob/03543e8a06dea8c9bfb52963da8abbf1157d5d21/app/models/mailboxer/message.rb
Based on this, what should I modify to have it so when I reply to a user, the reply action is called, and then conversations "calls" messages which itself sets the sender_type to a value I want?
In the code you proposed, you are calling sender_type
on the array of messages, but that method exists on the Mailboxer::Message
model. As it's a polymorphic association see code of model here, the sender
of the Mailboser::Message
will be the user that was used for creating the message (in this case current_user
).
So I guess what you are looking for, is to get the user_type
of your sender for a specific message of a conversation.
So this is can be done, by doing the following (in this example let's get the user_type
of the user that sent the first message of a given conversation :
conversation.messages.first.sender.user_type