Rails:管理已读/未读消息

对于类似论坛的应用程序,我需要能够向每个用户显示未读(新)消息。 是否有任何Rails插件/ gem用于此目的?

或者:有没有任何提示以有效的方式完成这项工作? 我正在考虑一个额外的数据库表,为每个用户存储每条消息的未读状态(或读状态?)。 但这似乎是蛮力的方法,也许有一个更聪明的解决方案……

祝愿,乔治

与此同时,我根据上面的想法创建了一个Rails插件,但是使用了更加改进的算法。 看看这里:
http://github.com/ledermann/unread

它可能涉及到谁,这是我的解决方案。 我添加了一个用于存储读取状态的新表。 此表中的一行给出了用户在给定主题中到目前为止已阅读的post数。 如果主题有更多post,那么用户已经阅读,它被称为“未读”主题。

用法:

# Get number of unread posts of the logged-in user: Discussion.unread_by(current_user).count # => 42 # Mark a topic as read some_topic = Discussion.find(x) some_topic.read_by!(current_user) 

简单模型“ReadStatus”:

 class ReadStatus < ActiveRecord::Base belongs_to :user belongs_to :discussion validates_presence_of :user_id, :discussion_id, :post_count end 

从模型“讨论”中提取,用于存储主题和post:

 class Discussion < ActiveRecord::Base belongs_to :user belongs_to :topic, :class_name => 'Discussion', :foreign_key => 'discussion_id' named_scope :unread_by, lambda { |user| { :joins => "LEFT JOIN read_statuses ON (read_statuses.user_id = #{user} AND read_statuses.discussion_id = discussions.id)", :conditions => "discussions.discussion_id IS NULL AND (read_statuses.user_id IS NULL) OR (read_statuses.post_count < discussions.post_count)", :order => 'discussions.updated_at DESC' } } def read_by!(user) if read_status = ReadStatus.first(:conditions => { :user_id => user.id, :discussion_id => self.id }) read_status.update_attributes! :post_count => self.post_count else ReadStatus.create! :user_id => user.id, :discussion_id => self.id, :post_count => self.post_count end end end