0
@followers = current_user.followers.joins(:received_messages).uniq.order("id DESC")

快速解释,它抓取当前用户的关注者并将他们与消息一起发送/接收。并且一次只显示一个。

我正在使用它来显示收件箱中的“消息”,该部分工作正常,我想通过最新发送或接收的消息 ID 订购该收件箱。

现在我的视图看起来像这样,(这真是一团糟,我知道有一些方法可以让我在 ruby​​ 中做到更清洁)

-if f.sent_messages.first && f.received_messages.first
  -if f.sent_messages.last.id > f.received_messages.last.id
    p This is running
    =image_tag f.avatar
    h2= f.uid
    p= f.sent_messages.last.body
    =link_to "Go To Conversation", conversation_path(:id => f.sent_messages.last.sender_id)
  -else
    =image_tag f.avatar
    h2= f.uid
    p= f.received_messages.last.body
    =link_to "Go To Conversation", conversation_path(:id => f.received_messages.last.receiver_id)
  end
-elsif f.sent_messages.first && f.received_messages.first == nil
  =image_tag f.avatar
  h2= f.uid
  p= f.sent_messages.last.body
  =link_to "Go To Conversation", conversation_path(:id => f.sent_messages.last.sender_id)
-elsif f.sent_messages.first == nil && f.received_messages.first
  =image_tag f.avatar
  h2= f.uid
  p= f.received_messages.last.body
  =link_to "Go To Conversation", conversation_path(:id => f.received_messages.last.receiver_id)
4

1 回答 1

0

尝试这个

-if f.sent_messages.any? || f.received_messages.any?
  =image_tag f.avatar
  %h2 = f.uid
  -unless f.sent_messages.empty? || f.received_messages.empty?
    -message,id = f.sent_messages.last.id > f.received_messages.last.id ? [f.sent_messages.last,f.sent_messages.last.sender_id] : [f.received_messages.last, f.received_messages.last.receiver_id]
  -else
    -message,id = f.sent_messages.last.nil? ? [f.received_messages.last,f.received_messages.last.receiver_id] : [f.sent_messages.last,f.sent_messages.last.sender_id]
  %p = message.body
  =link_to "Go To Conversation", conversation_path(:id => id)
于 2013-11-14T21:40:23.263 回答