# Easy Date question - newb

**URL:** <https://www.ruby-forum.com/t/easy-date-question-newb/88640>\
**Category:** Rails\
**Created:** [March 13, 2007, 9:48am UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640 "2007-03-13T09:48:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 9:48am UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/1 "2007-03-13T09:48:34Z")

</div>

All I want to do is find the objects that were created today under the  
‘created\_on’ column. I’ve tried many ways, here is one example using  
the chronic gem.

@products = Product.find\_all\_by\_created\_on(Chronic.parse(‘today’))

I must be missing something right in front of me … Thanks for any  
help.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 10:56am UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/2 "2007-03-13T10:56:03Z")

</div>

jko170 wrote:

> All I want to do is find the objects that were created today under the  
> ‘created\_on’ column. I’ve tried many ways, here is one example using  
> the chronic gem.
> 
> @products = Product.find\_all\_by\_created\_on(Chronic.parse(‘today’))
> 
> I must be missing something right in front of me … Thanks for any  
> help.

Assuming your database is mysql (this may hold for others, I don’t know)  
then when comparing a date eg 2007-03-25 to a datetime, mysql extends  
the date to midnight on that day. I’m not sure what time of day Chronic  
returns when you do Chronic.parse(‘today’) but it sure as hell ain’t  
midnight:

irb(main):006:0\> Chronic.parse(‘today’)  
=\> Tue Mar 13 17:00:00 GMT 2007  
irb(main):007:0\> Chronic.parse(‘tomorrow’)  
=\> Wed Mar 14 12:00:00 GMT 2007

@products = Product.find\_all\_by\_created\_on(Date::today)  
@products =  
Product.find\_all\_by\_created\_on(Chronic.parse(‘today’).to\_date)  
@products = Product.find\_all\_by\_created\_on(Chronic.parse(‘today’).change  
:hour =\> 0)

Should all work

Fred

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/3 "2007-03-13T12:00:52Z")

</div>

hmm. None of the suggestions worked. I just created some new objects  
so the created\_on date should be set to today. I also changed it to  
Product.find(:all) and its passing through to my view okay. All of the  
suggestions rendered an empty unordered list in my view, which I’m  
guessing means empty array.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/4 "2007-03-13T21:00:38Z")

</div>

This is driving me absolutely nuts! Thanks for the reply Rob. In AWDwR  
it says “Rails applications conventionally use the \_on suffix for date  
columns and the \_at suffix for columns that include time.” Date and  
time is what I want so I changed the column to created\_at and  
restarted the server. I also changed my controller and view to reflect  
‘created\_at’ but I still keep getting the same problem. I can find the  
objects if I just use Object.find(:all) but any of the other  
suggestions returns an empty arrray. I’ve made sure the objects were  
created today. I’m guessing that the dates in my controller and in the  
database are just not aligning up. Thanks for the help so far.  
Here is my code:

Controller —  
def index  
@products =  
Product.find\_all\_by\_created\_at(Time.now.beginning\_of\_day())

```
respond_to do |format|
  format.html # index.rhtml
end

```

end

View —

\<% for product in @products %\> - \<%= product.created\_at %\> 
\<% end %\>

My model and view has nothing special in it that would conflict with  
this.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/5 "2007-03-13T13:41:07Z")

</div>

On Mar 13, 2007, at 6:59 AM, jko170 wrote:

> hmm. None of the suggestions worked. I just created some new objects  
> so the created\_on date should be set to today. I also changed it to  
> Product.find(:all) and its passing through to my view okay. All of the  
> suggestions rendered an empty unordered list in my view, which I’m  
> guessing means empty array.

I have a model that does something similar:

def self.count\_recent(time\_span=1.hour)  
count(:conditions =\> [ “updated\_at \>= ?”, time\_span.ago.to\_s  
(:db) ])  
end

Where to show the number that have been updated today, I call this as:  
\<%= “(#{pluralize Product.count\_recent  
(Time.now.seconds\_since\_midnight), ‘product’} today)” %\>

In your case, perhaps this:  
@products = Product.find\_all\_by\_created\_on  
(Time.now.beginning\_of\_day())  
Or if your database timestamps are UTC:  
@products = Product.find\_all\_by\_created\_on  
(Time.now.utc.beginning\_of\_day())

I tend to use the created\_at rather than created\_on, but I think this  
would work for you.

-Rob

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

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/6 "2007-03-13T21:09:42Z")

</div>

On Mar 13, 2007, at 3:57 PM, jko170 wrote:

> Here is my code:
> 
> Controller —  
> def index  
> @products =  
> Product.find\_all\_by\_created\_at(Time.now.beginning\_of\_day())  
> Try changing this line to:

@products = Product.find(:all, :conditions =\> [ ‘created\_at \>= ?’,  
Time.now.beginning\_of\_day.to\_s(:db) ])

You want _since_ midnight, not _ONLY_ at midnight, right?

> ```
> <%= product.created_at %>
> 
> ```
> 
> \<% end %\>
> 
> My model and view has nothing special in it that would conflict with  
> this.

-Rob

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

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 13, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/easy-date-question-newb/88640/7 "2007-03-13T21:32:46Z")

</div>

That’s it! So before I was querying a date and time literally only at  
the beginning of the day (12am). Thanks Rob I really appreciate it.
