문제

I am using the mailboxer gem to send messages within my ruby on rails app. The gem is working. I can't for the life of me figure out how to access the recipient on the sent messages page. On the sent messages page, I am trying to loop through sent messages, and access the recipient of the sent messages and display that person to the user.

On the regular inbox page, I used the following:

<% all_mail = current_user.mailbox.inbox %>
<% all_mail.each do |letter| %>
    <tr>
        <td><%= letter.messages.last.sender.email %></td>
        <td><%= letter.messages.last.subject %></td>
   </tr>
<% end %>

I am trying to make a similar look for the sent messages page, but I can't figure out how to do it(specifically access the email of the recipient).

I can't use:

#user wants to retrieve all his conversations
user.mailbox.conversations

#user wants to retrieve his inbox
user.mailbox.inbox

#user wants to retrieve his sent conversations
user.mailbox.sentbox

because there is no way to organize how the messages are displayed using those methods (at least as far as I know). How can I loop through the messages and find the message recipient for each message?

도움이 되었습니까?

해결책

You have the participants in conversations (you call it letters).

Just filter the current user and you'll be good.

conversation = user.mailbox.sentbox.first
filtered_participants = conversation.participants.reject do |u| 
  u.id == current_user.id
end

다른 팁

Each message has recipients, which is a list. Assuming that your conversations are just between two users you can grab for the first list's element:

<% conversation.messages.each do |message| %>
  <%= message.recipients.first.email %>, <%= message.body %>
<% end %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top