# Querying with Ruby date ranges

**URL:** <https://www.ruby-forum.com/t/querying-with-ruby-date-ranges/175165>\
**Category:** Rails\
**Created:** [September 9, 2009, 8:33am UTC](https://www.ruby-forum.com/t/querying-with-ruby-date-ranges/175165 "2009-09-09T08:33:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5e9695/32.png) [@Bob\_O](https://www.ruby-forum.com/u/Bob_O)\
**Post date:** [September 9, 2009, 8:33am UTC](https://www.ruby-forum.com/t/querying-with-ruby-date-ranges/175165/1 "2009-09-09T08:33:55Z")

</div>

hello,

im stuck, im trying to do a find on a model with a date range where i  
only have 1 date.

example something like

def self.date\_range(posted\_date)  
date\_range = find(:all, :conditions =\> { :received\_date =\>  
posted\_date…24.hours })  
return date\_range  
end

basically i pass in the posted date from another object and get  
responses back based on the responses where the response\_date = the  
post\_date + 24.hours or something along those lines.

any help would be great, hopefully i explained it ok.

thanks

---

<div class="post-metadata">

**Author:** ![Bob\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5e9695/32.png) [@Bob\_O](https://www.ruby-forum.com/u/Bob_O)\
**Post date:** [September 9, 2009, 9:16am UTC](https://www.ruby-forum.com/t/querying-with-ruby-date-ranges/175165/2 "2009-09-09T09:16:39Z")

</div>

This should help:

> **[Querying for a date range (Ruby on Rails)](https://morecode.wordpress.com/2007/01/14/querying-for-a-date-range-ruby-on-rails/)**
>
> Date ranges are very odd when you first scratch your head at them. Sometimes the first thought that comes to mind might be a solution that would select every single date, one by one in a loop. It d…

---

<div class="post-metadata">

**Author:** ![Bob\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5e9695/32.png) [@Bob\_O](https://www.ruby-forum.com/u/Bob_O)\
**Post date:** [September 9, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/querying-with-ruby-date-ranges/175165/3 "2009-09-09T13:44:50Z")

</div>

Ruby handles date ranges as integers internally so it is a bad idea to  
try that. You can do something simpler using the array syntax of  
conditions

:conditions =\> [“received\_date \>= ? and received\_date  
\<= ?”,posted\_date, posted\_date]

This will form the necessary SQL for the database query.

---

<div class="post-metadata">

**Author:** ![Bob\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5e9695/32.png) [@Bob\_O](https://www.ruby-forum.com/u/Bob_O)\
**Post date:** [September 9, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/querying-with-ruby-date-ranges/175165/4 "2009-09-09T19:37:33Z")

</div>

Mukund wrote:

> Ruby handles date ranges as integers internally so it is a bad idea to  
> try that. You can do something simpler using the array syntax of  
> conditions
> 
> :conditions =\> [“received\_date \>= ? and received\_date  
> \<= ?”,posted\_date, posted\_date]
> 
> This will form the necessary SQL for the database query.

Or even better, “received\_date between ? and ?”.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
