# About active\_record timestamps

**URL:** <https://www.ruby-forum.com/t/about-active-record-timestamps/191616>\
**Category:** Rails\
**Created:** [July 15, 2010, 11:57am UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616 "2010-07-15T11:57:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 15, 2010, 11:57am UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/1 "2010-07-15T11:57:44Z")

</div>

hi  
I want to build a filter that gets records from database based on  
selected date.such as

#### 

Product.find(:all, :conditions =\> [“created\_at = ?”,selected\_date])

#### 

1 If selected\_date is a Date object while created\_at is a DateTime  
object,is it work?  
2 Do i need to set this?

#### 

ActiveRecord::Base.default\_timezone = :utc

#### 

My talbe has another date type field which i want it to be local  
timezone,so it seems i should not set  
ActiveRecord::Base.default\_timezone = :utc,but my filter will not works  
well because of the default local timezone setting for the createdz\_at  
attribute.How to resolve these problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 15, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/2 "2010-07-15T19:19:38Z")

</div>

On Jul 15, 2010, at 2:57 AM, Guo Y. wrote:

> hi  
> I want to build a filter that gets records from database based on  
> selected date.such as
> 
> #### 
> 
> Product.find(:all, :conditions =\> [“created\_at = ?”,selected\_date])
> 
> #### 
> 
> 1 If selected\_date is a Date object while created\_at is a DateTime  
> object,is it work?  
> 2 Do i need to set this?

It will work just fine. However, it won’t do what you want…  
created\_at is going to contain date/time down to the second (or more).  
If you pass in a date of “2010-07-15” it’s going to append a time of  
“00:00:00” (most likely). So you’re not going to get records created  
anytime during that day.

I’m guessing that’s what you want. If so you need to modify that to  
check for any record created on that _date_ using a range or something  
else.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 15, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/3 "2010-07-15T21:11:29Z")

</div>

On Jul 15, 2010, at 1:17 PM, Philip H. wrote:

> > #### 
> > 
> > ## Thanks!

Try:

Product.find(:all, :conditions =\> [“DATE(created\_at) = ?”,  
selected\_date])

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 15, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/4 "2010-07-15T21:57:44Z")

</div>

On Thu, Jul 15, 2010 at 10:09 PM, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ## 1 If selected\_date is a Date object while created\_at is a DateTime for any record created on that _date_ using a range or something else. Thanks!
> 
> Try:
> 
> Product.find(:all, :conditions =\> [“DATE(created\_at) = ?”, selected\_date])
> 
> -Rob

But have in mind that this wont use the created\_at index (if you have  
one)

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 15, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/5 "2010-07-15T21:57:45Z")

</div>

On 15 July 2010 20:09, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > ## 1 If selected\_date is a Date object while created\_at is a DateTime for any record created on that _date_ using a range or something else. Thanks!
> 
> Try:
> 
> Product.find(:all, :conditions =\> [“DATE(created\_at) = ?”, selected\_date])

Will that have to do a calculation on every record to determine DATE?  
If so would it be more efficient to specify a range for created\_at  
instead (something like \>= selected\_date and \< selected\_date + 1.day)?  
Particularly if there were an index on the column.

Colin

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 15, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/6 "2010-07-15T22:08:17Z")

</div>

> > > is going to contain date/time down to the second (or more). If you pass in
> > > 
> > > > My talbe has another date type field which i want it to be local
> > 
> > Product.find(:all, :conditions =\> [“DATE(created\_at) = ?”, selected\_date])
> 
> Will that have to do a calculation on every record to determine DATE?

Yes. And as Christos said, you lose the use of the index.

> If so would it be more efficient to specify a range for created\_at  
> instead (something like \>= selected\_date and \< selected\_date + 1.day)?  
> Particularly if there were an index on the column.

d = Time.now  
Product.find(:all, :conditions =\> [“created\_at BETWEEN ? AND ?”,  
d.beginning\_of\_day, d.end\_of\_day])

would work…

-philip

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 16, 2010, 6:49am UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/7 "2010-07-16T06:49:52Z")

</div>

On Thu, Jul 15, 2010 at 12:09 PM, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try:
> 
> Product.find(:all, :conditions =\> [“DATE(created\_at) = ?”, selected\_date])

Note: this makes an assumption about what DB you’re using – e.g.  
Oracle has no such function and will throw an error 🙂

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 16, 2010, 10:36am UTC](https://www.ruby-forum.com/t/about-active-record-timestamps/191616/8 "2010-07-16T10:36:10Z")

</div>

Thank you! it make me confident.
