Rails:保持用户未读通知的数量 [英] Rails: Keeping count of users unread notifications

查看:53
本文介绍了Rails:保持用户未读通知的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个处理用户活动通知系统的活动模型.活动观察者在发生某些操作(例如创建新文章)时创建一个新活动.现在我想记录下当前用户没有看到的这些活动通知中有多少(类似于 facebook 上的通知宝石).每次用户点击他们的通知链接时,数字应该重置为 0,每个创建的通知都应该将计数增加 1.我应该在哪里为每个用户存储这些数据?使用会话有效还是其他更好的方法?

I currently have an activity model that handles a user activity notification system. An activity observer creates a new activity when some action happens (such as a new article being created). Now I want to keep a record of how many of these activity notifications the current user has not seen (similar to the notification jewel on facebook). Every time the user clicks on their notifications link the number should reset to 0 and every notification created should increase the count by 1. Where would I store this data for each user? Would using a session work or is something else better?

推荐答案

对此有几种可能的方法:规范化和非规范化.我将从规范化开始:

There are a couple possible approaches to this: normalized and denormalized. I'll start with normalized:

听起来您在这里使用了三个模型:活动、通知(user_activities")和用户.如果我的假设有误,请纠正我:

It sounds like you have three models in play here: activities, notifications ("user_activities"), and users. Correct me if I'm wrong in my assumptions here:

  • Activitybelongs_to :user 和 has_many :notifications(一个用户执行一个活动,多个用户收到该活动的通知)
  • 通知belongs_to :activity 和belongs_to :user 并具有读取"属性/标志
  • 用户 has_many :notifications

在活动的 after_create 回调中,模型应确定哪些用户需要该活动的通知,然后通过为每个用户创建通知对象来通知他们.

In an after_create callback on activity, the model should determine what users need notifications of that activity, and then notify them by creating notification objects for each of them.

关于通知,您可以创建一个名为 unread 的类方法,它指定活动的读取标志为 false 的条件:

On notifications, you can create a class method called unread which specifies a condition that the activity's read flag is false:

def self.unread
  where(:read => false)
end

然后,要访问用户的未读通知计数,只需调用:

Then, to access a user's unread notifications count, simply call:

user.notifications.unread.count

当用户查看他的通知时,调用:

When a user views his notifications, call:

user.notifications.unread.update_all(:read => true)

方法 2:非规范化

在这种方法中,每当创建一个活动时,它应该为每个通知的用户手动增加一个计数器.您可以通过以下任一方式完成此操作:

Approach 2: Denormalized

In this approach, whenever an activity is created, it should manually increment a counter for each notified user. You can accomplish this with either:

  • 用户的unseen_count"属性
  • 非关系型数据库(例如 redis)中的键值对

活动中:

def users_to_notify
  # Find a list of users to notify
end

def notify_users(users)
  users.each &:notify
end

def after_create
  notify_users(users_to_notify)
end

在用户中:

def notify
  update_attributes(:unseen_count => unseen_count + 1)
end

def see_activities
  update_attributes(:unseen_count => 0)
end

这种方法的缺点是您消除了通知模型,因此用户只有通知的原始计数,而无法查看通知及其相关活动的详细列表.您可以使用混合方法,但请记住,处理不可见通知计数的两个真实来源是有风险的.

The downside to this approach is you've eliminated the Notification model, so a user only has a raw count of notifications, and can't view a detailed list of notifications and their associated activities. You could use a hybrid approach, but remember that it's risky to deal with two sources of truth for unseen notification count.

附带说明:在 observer 而不是直接在模型上使用 after_create :

On a side note: it may make more sense to call notify_users in an observer instead of an after_create directly on the model:

class ActivityObserver < ActiveRecord::Observer
  def after_create(activity)
    activity.users_to_notify.each &:notify
  end
end

如果您使用观察者,则可以删除 Activity#notify_users 和 Activity#after_create.

If you use the observer, you can remove Activity#notify_users and Activity#after_create.

这篇关于Rails:保持用户未读通知的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆