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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:42:15+00:00 2026-05-29T03:42:15+00:00

I have a few Mongoid model classes that all have a :datetime field. I

  • 0

I have a few Mongoid model classes that all have a :datetime field. I would like to query these models to find out records that fall under today’s date. The query would look like this:

scope today, where(:datetime.gt => DateTime.now.beginning_of_day, :datetime.lt => DateTime.now.end_of_day)

Currently, the above scope code are duplicated within all the model classes. How do I dry this up?

  • 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-29T03:42:22+00:00Added an answer on May 29, 2026 at 3:42 am

    The simples thing is:

    module TimeDepedentent
      field :datetime
    
      scope today, where(:datetime.gt => DateTime.now.beginning_of_day, :datetime.lt => DateTime.now.end_of_day)
    
      scope yesterday ...
      scope one_month_ago ...
    end
    
    class MyModel
      include TimeDepedentent
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In almost all of my projects I have few files that are project wide
I am trying to retrieve all cards from Mongoid that have request=preview I'm doing
I have few arrays of ccspirit images, these are balloons of different colors, like
i have few texboxt and dropdownlist, which their id would be something like txtName1,
I have a model like this: class Search include Mongoid::Document embeds_many :terms accepts_nested_attributes_for :terms
To test rails/mongoid DB models until now, I used to have a real model
I have few different applications among which I'd like to share a C# enum.
We have few components like libraries dlls When initially created I ran the following
I've been struggling to create a form for a Mongoid model that has an
I have few 'so' files that I need to load in maven java project

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.