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 6372367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:13:44+00:00 2026-05-25T01:13:44+00:00

I store user activity data: when user visited current article, topic or personal message

  • 0

I store user activity data: when user visited current article, topic or personal message to show him how many new comments and messages were added while he was offline.

class SiteActivity
  include Mongoid::Document
  include Mongoid::Timestamps
  belongs_to :user
  belons_to :activity, polymorphic: true
end

In this case I store one record per document.

Another option is to use embedded documents, so all user activities will be stored in one document:

class SiteActivity
  include Mongoid::Document
  belongs_to :user
  embeds_many :user_activities
  validates :user_id, uniqueness: true
end

class UserActivity
  include Mongoid::Document
  include Mongoid::Timestamps
  embedded_in :site_activity
  belongs_to :activity, polymorphic: true
end

So now I don’t need to search through all SiteActivities (many many records) but I can fetch one user_activity for current_user and find activity I need through it embedded documents.

Which way is more efficient to store and search data?

My ordinary use case is:

I have got a user and a post so I am fetching for site_activity with this data to see the date when this user visited post last time.

with my first option:

activity = SiteActivity.where(user_id: current_user.id, activity_id: post.id, activity_type: post.class)

with second

user_activity = SiteActivity.where(user_id: current_user.id)
activity = user_activity.user_activities.where(activity_id: post.id, activity_type: post.class)
  • 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-05-25T01:13:45+00:00Added an answer on May 25, 2026 at 1:13 am

    It would be better to use the first approach(individual documents) and use a capped collection if possible, as you don’t want to have rapidly growing collection(mongoid will have support for capped collections in 2.2, which would be out this weekend I guess).

    The second approach(embedded documents), you will need to first fetch the root document for the user and then traverse the array in application to find the activity related with post you are looking for. Mongoid may make it look like everything is done in db due to similarity of syntax in finding an embedded document, but its really iterating the array.

    As you already have the user_id, activity_id and activity_type before making a query, and you would not want the whole list of activities for the user to be retrieved from db when you are looking for a particular activity, I will prefer first case. There would be much less calculations(searching) in application and there will be much less network traffic.

    With individual documents approach, it would be great if you also create a unique index on user_id, activity_id, activity_type. It will help you contain the number of documents. You can have the uniqueness validation(extra query), but that would be mostly unnecessary if you have the unique index. The only benefit of validation will be an validation error if there are duplicates, but index will ignore duplicate entries silently unless you persist in safe mode.

    In case you also want the historical site activity to be persisted, you can have the structure like:

    class SiteActivity
      include Mongoid::Document
      include Mongoid::Timestamps
      belongs_to :user
      belongs_to :activity, polymorphic: true
    
      index [:user_id, :activity_id, :activity_type], :background => true, :unique => true
    
      field :last_access_time, :type => Time
      # last_access_times just here for history, not used
      field :last_access_times, :type => Array, :default => []
    end
    
    activity = SiteActivity.find_or_initialize_by(:user_id => current_user.id,
                   :activity_id => post.id, :activity_type => post.class)
    time = Time.now.utc
    activity.last_access_time = time
    activity.last_access_times << time
    activity.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store user entered changes to a particular table, but not show
Where do you store user-specific and machine-specific runtime configuration data for J2SE application? (For
I'm a new Windows programmer and I'm not sure where I should store user
I asked about Choosing a method to store user profiles the other day and
I'm trying to generate a unique ID in php in order to store user-uploaded
Is there an equivalent of the OS X Keychain, used to store user passwords,
I need to store the user's password in my iphone app. When posting an
I'm using nhibernate to store some user settings for an app in a SQL
What is the best way to store a user name and password for a
How can I use a database and PHP sessions to store a user's shopping

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.