Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:37:08+00:00 2026-06-12T22:37:08+00:00

I currently have an activity model that handles a user activity notification system. An

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:37:09+00:00Added an answer on June 12, 2026 at 10:37 pm

    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:

    • Activity belongs_to :user and has_many :notifications (one user performs an activity and multiple users get notified of the activity)
    • Notification belongs_to :activity and belongs_to :user and has a “read” attribute / flag
    • User has_many :notifications

    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:

    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)
    

    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:

    • An “unseen_count” attribute on user
    • A key-value pair in a non-relational database (e.g. redis)

    In Activity:

    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
    

    In User:

    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.

    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
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a class called clientActivity that contains a finger paintig activity that
I am currently working on an android project and I have an activity, lets
I have a TabbedView Activity with 3 tabs. Currently, only the 1st tab has
I'am currently working on GWTs Activity-Place implementation. Now I have found problem with the
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
I have an activity that shows customer information. All the controls are drawn dynamically
I have an app that is in portrait mode, but some screens should have
In my application, I have specified a second activity that can be launched from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.