Mysql 未读邮件计数器

Mysql 未读邮件计数器,mysql,ruby-on-rails,ruby-on-rails-4,private-pub,Mysql,Ruby On Rails,Ruby On Rails 4,Private Pub,我正在rails 4上创建一个简单的聊天应用程序。控制器、模型和视图已创建,但功能仍不完整。我的数据库中有两个表:对话和消息。对话表包含两个字段,发送者id和接收者id。消息表包含三个字段:正文、用户id和读取(默认为0表示未读取) 型号: class Conversation < ActiveRecord::Base belongs_to :sender, :foreign_key => :sender_id, :class_name => "User" b

我正在rails 4上创建一个简单的聊天应用程序。控制器、模型和视图已创建,但功能仍不完整。我的数据库中有两个表:对话和消息。对话表包含两个字段,发送者id和接收者id。消息表包含三个字段:正文、用户id和读取(默认为0表示未读取)

型号:

class Conversation < ActiveRecord::Base

    belongs_to :sender, :foreign_key => :sender_id, :class_name => "User"
    belongs_to :reciever, :foreign_key => :reciever_id, :class_name => "User"

    has_many :messages, :dependent => :destroy

    validates_uniqueness_of :sender_id, :scope => :reciever_id

    scope :involving, lambda { |user_id|
        where("sender_id = ? OR reciever_id = ?", user_id, user_id)
    }

    scope :between, lambda { |sender_id, reciever_id|
        where("(sender_id = ? AND reciever_id = ?) OR (sender_id = ? AND reciever_id = ?)", sender_id, reciever_id, reciever_id, sender_id)
    }

    def other_interlocutor(user_id)
        if sender.id == user_id
            return reciever.id
        else
            return sender.id
        end
    end
end

class Message < ActiveRecord::Base
  belongs_to :conversation
  belongs_to :user

  validates_presence_of :conversation_id, :user_id, :body

end
我有一个页面,其中列出了所有用户的对话,单击了一个对话,也列出了与该对话相关的所有消息。在同一页上,我订阅了每个
对话\u消息\u路径
,为每个对话创建单独的频道。每当发送消息时,都会呈现一个
create.js.erb
文件,其中我将发布到那些订阅的频道:

<% publish_to conversation_messages_path(@conversation.id) do %>
    $("#conversations_link").text("<%= current_user.unread_messages_count %> Conversations");
    $("#messages").append("<%= escape_javascript render(:partial => 'message', :locals => { :message => @message })%>");
<% end %>

$(“对话链接”)。文本(“对话”);
$(“#messages”).append(“'message',:locals=>{:message=>@message})%>”;
$(“#对话链接”)
是我想显示未读邮件计数的地方

当前,未读邮件计数返回错误计数,只有当
对话.sender\u id
向接收方发送邮件时,导航栏才会更新

我的未读邮件计数器未返回正确的未读邮件数。我不知道怎么修理它。我的代码出了什么问题?
谢谢。

我认为你的领域建模真的很糟糕

谈话的整体理念是,双方轮流充当发送者和接收者。你模仿的是独白

独白是一个人发表的演讲,或者是一段长的单面对话 让你想从无聊中解脱出来的对话。 希腊词根monologos翻译成“单独说话”,并且 这是一个独白:一个人做所有的谈话

您在此处使用的域模型应如下所示:

它实际上是链接到两个(或更多)用户的消息:
发送者
接收者
。为了简单起见,我在这里坚持1:1的消息传递方式(与可能属于多个收件人的群聊相比)

注意,当不能从关联名派生类和外键时,我们需要告诉Rails该类和外键

<> >而不是拥有布尔<代码> Read 字段,您可能需要考虑使用A来表示消息的状态。

枚举基本上是一个整数列,映射到符号列表

class Message
  enum :status, [:unread, :read] 
  belongs_to :recipient, class_name: 'User'
  belongs_to :sender, class_name: 'User'
  belongs_to :conversation
end
枚举为您提供如下范围:

Message.unread
Message.read
和条件,例如:

message.unread?
message.read?
如果您想添加更多的状态,例如
:archived
:trashed
,那么它将非常简单

有了它,你就不需要你的
未读邮件数了。这将消耗大量内存,因为您从数据库中提取记录只是为了计算相关记录

current_user.messages.unread.size
此外,我们还应正确定义用户和对话之间的关系:

class Conversation
  has_many :messages
  has_and_belongs_to_many :users
end

class Users
  # ..
  has_and_belongs_to_many :conversations
end
此关系将用户和会话存储在
users\u conversations
join表中。可以使用以下生成器创建联接表迁移:

rails generate migration users_conversations
补充: 在视图中使用
session[:user\u id]
是一种非常糟糕的代码味道。您在整个应用程序中紧密耦合身份验证逻辑

而是创建一个助手:

class SessionsHelper
  def current_user
    @current_user ||= User.find(session[:user_id])
  end

  def user_signed_in?
     !current_user.nil?
  end 
end

应用程序的其他部分不应该知道您在
会话[:user\u id]
中存储了当前用户,只是存在一个
当前用户

,我认为您的域建模真的不正确

谈话的整体理念是,双方轮流充当发送者和接收者。你模仿的是独白

独白是一个人发表的演讲,或者是一段长的单面对话 让你想从无聊中解脱出来的对话。 希腊词根monologos翻译成“单独说话”,并且 这是一个独白:一个人做所有的谈话

您在此处使用的域模型应如下所示:

它实际上是链接到两个(或更多)用户的消息:
发送者
接收者
。为了简单起见,我在这里坚持1:1的消息传递方式(与可能属于多个收件人的群聊相比)

注意,当不能从关联名派生类和外键时,我们需要告诉Rails该类和外键

<> >而不是拥有布尔<代码> Read 字段,您可能需要考虑使用A来表示消息的状态。

枚举基本上是一个整数列,映射到符号列表

class Message
  enum :status, [:unread, :read] 
  belongs_to :recipient, class_name: 'User'
  belongs_to :sender, class_name: 'User'
  belongs_to :conversation
end
枚举为您提供如下范围:

Message.unread
Message.read
和条件,例如:

message.unread?
message.read?
如果您想添加更多的状态,例如
:archived
:trashed
,那么它将非常简单

有了它,你就不需要你的
未读邮件数了。这将消耗大量内存,因为您从数据库中提取记录只是为了计算相关记录

current_user.messages.unread.size
此外,我们还应正确定义用户和对话之间的关系:

class Conversation
  has_many :messages
  has_and_belongs_to_many :users
end

class Users
  # ..
  has_and_belongs_to_many :conversations
end
此关系将用户和会话存储在
users\u conversations
join表中。可以使用以下生成器创建联接表迁移:

rails generate migration users_conversations
补充: 在视图中使用
session[:user\u id]
是一种非常糟糕的代码味道。您在整个应用程序中紧密耦合身份验证逻辑

而是创建一个助手:

class SessionsHelper
  def current_user
    @current_user ||= User.find(session[:user_id])
  end

  def user_signed_in?
     !current_user.nil?
  end 
end

应用程序的其他部分不应该知道您将当前用户存储在
会话[:user\u id]
中,只是有一个
当前用户

我希望我在这里不要显得过于苛刻-我的目的不是批评您的能力,而是告诉您,您的问题是由于设计中的一些基本缺陷造成的。金融机构