Rails 4:计算未读消息

时间:2014-08-12 17:41:38

标签: ruby-on-rails

我正在尝试构建一个简单的邮件系统。 如何在他们参与的所有会话中向每个用户显示未读消息总数? 此外,当用户进入' /对话'如何识别并标记每个具有current_user未读取的消息的会话?

到目前为止,我有这个:

class Message < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation, :counter_cache => true
end

class Conversation < ActiveRecord::Base
  belongs_to :user  
  has_many :messages

  belongs_to :recipient,
   :class_name => 'User',
   :primary_key => 'user_id',
   :foreign_key => 'recipient_id'
end

class User < ActiveRecord::Base  
  has_many :conversations
  has_many :messages, :through => :conversations 
end

模式

  create_table "conversations", force: true do |t|
    t.integer  "user_id"
    t.integer  "recipient_id"
    t.integer  "messages_count", default: 0
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "messages", force: true do |t|
    t.integer  "user_id"
    t.integer  "conversation_id"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

0 个答案:

没有答案