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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:23:19+00:00 2026-05-31T09:23:19+00:00

I have many User objects with created_at attribute e.g. I get objects with @users

  • 0

I have many User objects with created_at attribute e.g.

I get objects with @users = User.all

I want get the count of User objects with various ages from creation with

@users.size

for these date ranges:

yesterday
last week
last month
last year.

How can I do it?

I use mongoid.

  • 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-31T09:23:20+00:00Added an answer on May 31, 2026 at 9:23 am

    You can write scopes for this:

    class User
      include Mongoid::Document
      ...
    
      ## Scopes for calculating relative users
      scope :created_yesterday, lambda { where(:created_at.gte => (Time.now - 1.day)) }
      scope :created_last_week, lambda { where(:created_at.gte => (Time.now - 1.week)) }
      scope :created_last_month, lambda { where(:created_at.gte => (Time.now - 1.month)) }
      scope :created_last_year, lambda { where(:created_at.gte => (Time.now - 1.year)) }
      ...
    end
    

    The reason we need to use a lambda here is that it delays the evaluation of the Time.now argument to when the scope is actually invoked. Without the lambda the time that would be used in the query logic would be the time that the class was first evaluated, not the scope itself.

    Now we can get the counts, by simply calling:

    User.created_yesterday.count
    User.created_last_week.count
    ...
    

    If you want the objects:

    @users_created_yesterday = User.created_yesterday
    

    You can use @users_created_yesterday in the views.

    Update

    Well with yesterday, if you mean time between, yesterday beginning of day 0:00 and yesterday end of day 23:59, you will have to take Time zones into consideration.

    Fo example, in your application.rb, if you have written:

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
     config.time_zone = 'Central Time (US & Canada)'
    

    If you have use this, all the times fetched by activerecord queries will be converted to this time zone, Central Time (US & Canada). In the database, all the times will be stored in UTC and will be converted to the time zone on fetching from the database. If you have commented out this line, default will be UTC. You can get all the time zones using the rake task rake time:zones:all or only the US timezones using rake time:zones:us.

    If you want the time zone specified in the application.rb, you need to use Time.zone.now, in the following code. If you use Time.now in the following code, you will get the time zone according to the time zone of your server machine.

    class User
      include Mongoid::Document
      ...
      scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) }
    
      class << self
        ## Class methods for calculating relative users
        def created_yesterday
          yesterday = Time.zone.now - 1.day
          created_between(yesterday.beginning_of_day, yesterday.end_of_day)
        end
    
        def created_last_week
          start_time = (Time.zone.now - 1.week).beginning_of_day
          end_time = Time.zone.now
          created_between(start_time, end_time)
        end
    
        def created_last_month
          start_time = (Time.zone.now - 1.month).beginning_of_day
          end_time = Time.zone.now
          created_between(start_time, end_time)
        end
    
        def created_last_year
          start_time = (Time.zone.now - 1.year).beginning_of_day
          end_time = Time.zone.now
          created_between(start_time, end_time)
        end
      end
    
      ..
      ..
    end
    

    So, you can write class methods and calculate start time and end time, supply it to the scope created_between, and you will be able to call them like User.created_yesterday, like we called before.

    Credits: EdgeRails. Since it is Mongoid, I had to look up at Mongoid docs

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

Sidebar

Related Questions

I have a many-to-many relationship between User s and Task s. I want the
I need to reformat my machine but I have so many user/passwords stored in
I have many many small images that are displayed on a user's profile and
I have many layers in the Documents that will be displayed on different user
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I have a static int that keeps an record of how many user control
I have two models - Tenant and User, each tenant will have_many users and
I have many controllers that will have some similar behavior, e.g. the user should
I have a form which have many check boxes in it. User will check
Given models User and Comments being populated such that a user may have many

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.