Ordering messages in conversation - rails

时光怂恿深爱的人放手 提交于 2019-12-20 07:12:37

问题


I'm using a gem called mailboxer to allow users to send messages among each other:

Here is what I have in my conversations controller:

class ConversationsController < ApplicationController
  ...

  private

  def mailbox
    @mailbox ||= current_user.mailbox
  end

  def conversation
    @conversation ||= mailbox.conversations.find(params[:id])
  end

  def conversation_params(*keys)
    fetch_params(:conversation, *keys)
  end

  def message_params(*keys)
    fetch_params(:message, *keys)
  end

  def fetch_params(key, *subkeys)
    params[key].instance_eval do
      case subkeys.size
      when 0 then self
      when 1 then self[subkeys.first]
      else subkeys.map{|k| self[k] }
      end
    end
  end
end

And this is what I have in my conversations show view:

<p> <%= conversation.subject %> </p>


 <%= content_tag_for(:p, conversation.receipts_for(current_user)) do |receipt| %>
    <% message = receipt.message %>


    <p>
      <%=  message.body %>
    </p>

  <% end %>


<%= render 'messages/form', conversation: conversation %>

Which works, but the messages are all scrambled. How do I order the messages of the conversation by when they were created?

My schema pertaining to mailboxer:

  create_table "receipts", force: true do |t|
    t.integer  "receiver_id"
    t.string   "receiver_type"
    t.integer  "notification_id",                            null: false
    t.boolean  "is_read",                    default: false
    t.boolean  "trashed",                    default: false
    t.boolean  "deleted",                    default: false
    t.string   "mailbox_type",    limit: 25
    t.datetime "created_at",                                 null: false
    t.datetime "updated_at",                                 null: false
  end

  add_index "receipts", ["notification_id"], name: "index_receipts_on_notification_id"


  create_table "conversations", force: true do |t|
    t.string   "subject",    default: ""
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
  end

  create_table "notifications", force: true do |t|
    t.string   "type"
    t.text     "body"
    t.string   "subject",              default: ""
    t.integer  "sender_id"
    t.string   "sender_type"
    t.integer  "conversation_id"
    t.boolean  "draft",                default: false
    t.datetime "updated_at",                           null: false
    t.datetime "created_at",                           null: false
    t.integer  "notified_object_id"
    t.string   "notified_object_type"
    t.string   "notification_code"
    t.string   "attachment"
    t.boolean  "global",               default: false
    t.datetime "expires"
  end

  add_index "notifications", ["conversation_id"], name: "index_notifications_on_conversation_id"

回答1:


I think you just need to order the receipts like this:

 <%= content_tag_for(:p, conversation.receipts_for(current_user).order(:created_at)) do |receipt| %>


来源:https://stackoverflow.com/questions/22417119/ordering-messages-in-conversation-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!