# ActiveRecord DateTime type search range

**URL:** <https://www.ruby-forum.com/t/activerecord-datetime-type-search-range/144834>\
**Category:** Rails\
**Created:** [August 2, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/activerecord-datetime-type-search-range/144834 "2008-08-02T21:54:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 2, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/activerecord-datetime-type-search-range/144834/1 "2008-08-02T21:54:11Z")

</div>

i have a table full of entries that each have a datetime field with a  
date and timestamp… how would i use activerecord to get all entries  
from say 2008-08-12 12:00:00 to 2009-08-12 12:00:00… i’ve tried  
searching the table in every way i can think of and can’t seem to find  
it online

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 2, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/activerecord-datetime-type-search-range/144834/2 "2008-08-02T22:07:51Z")

</div>

On 2 Aug 2008, at 20:54, Morgan M. wrote:

> i have a table full of entries that each have a datetime field with a  
> date and timestamp… how would i use activerecord to get all entries  
> from say 2008-08-12 12:00:00 to 2009-08-12 12:00:00… i’ve tried  
> searching the table in every way i can think of and can’t seem to find  
> it online

SomeClass.find :all, :conditions =\> {:date\_field =\>  
(first\_date…second\_date)}  
or  
SomeClass.find :all, :conditions =\> [‘date\_field \> ? and date\_field  
\< ?’, first\_date, second\_date]

Fred

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 3, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/activerecord-datetime-type-search-range/144834/3 "2008-08-03T18:58:14Z")

</div>

warning…  
I have been using range with ActiveSupport::TimeWithZone values  
(start\_at, end\_at) stored in DateTime columns in my DB  
check what this range will output : period = start\_at…end\_at  
vs period = Date.parse(start\_at.to\_s)…Date.parse(end\_at.to\_s)  
you will be surprised…
