Easy Date question - newb

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.

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

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.

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.

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
[email protected]

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
[email protected]

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.