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

  • Home
  • SEARCH
  • 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 1017051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:38:37+00:00 2026-05-16T10:38:37+00:00

I want to get models whom date is within a date range. So I

  • 0

I want to get models whom date is within a date range.
So I want to do something like

MyModel.find_all_by_field1_id_and_field2_id(value1, value2, :conditions => { :date => nb_days_ago..Date.yesterday })

The thing is, the date attribute of my model is a string (with the format “08-24-2010”), and I can’t modify this.
So to compare it to my range of dates, I tried this:

MyModel.find_all_by_field1_id_and_field2_id(value1, value2, :conditions => { Date.strptime(:date, "%m-%d-%Y") => nb_days_ago..Date.yesterday })

But I get an error that basically says that strptime can’t process the :date symbol. I think my solution is not good.

How can I compare my string to my range of dates ?

Thanks

  • 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-16T10:38:37+00:00Added an answer on May 16, 2026 at 10:38 am

    First of all I do not envy your situation. That’s a pretty ugly date format. The only thing I can think of is to generate an array of strings, in that format, representing ALL the days between your starting date and your finish date, then use the SQL “IN” syntax to find dates in that set (which you can do from within ActiveRecord’s :conditions param).

    For example, if you wanted to search to 10 days ago:

    num = 10 #number of days ago for search range
    # range starts at 1 because you specified yesterday
    matching_date_strings = (1..num).to_a.map{|x| x.days.ago.strftime("%m-%d-%Y")}
    => ["08-24-2010", "08-23-2010", "08-22-2010", "08-21-2010", "08-20-2010"]
    # then...
    records = MyModel.all(:conditions => { :date => matching_date_strings })
    # or in your case with field1 and field2
    records = MyModel.find_all_by_field1_id_and_field2_id(value1, value2, :conditions => { :date => matching_date_strings })
    

    The idea is this should generate SQL with something like “… WHERE date IN (“08-24-2010”, “08-23-2010”, “08-22-2010”, “08-21-2010”, “08-20-2010”)

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

Sidebar

Related Questions

I have a simple Python/Django class: class myModel(models.Model): date = models.DateTimeField() value = models.IntegerField()
I want to get the result in one list using three different models in
I want get data from one of my models. I am using Entity datamodel
var_dump of my array shows up like this. I want to get the count
I have some nested models that look something like: class Company has_many :managers end
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I want get the time used for a case so I can create an
I want get all of the Geom objects that are related to a certain
I want get as much as possible from Redis + Hiredis + libevent. I'm
What I want: get a xml from the AppData to use What I code

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.