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?
I currently have an activity model that handles a user activity notification system. An
Share
There are a couple possible approaches to this: normalized and denormalized. I’ll start with normalized:
Approach 1: Normalized
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:
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.
On notifications, you can create a class method called unread which specifies a condition that the activity’s read flag is false:
Then, to access a user’s unread notifications count, simply call:
When a user views his notifications, call:
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:
In Activity:
In User:
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.
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:
If you use the observer, you can remove Activity#notify_users and Activity#after_create.