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.

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 Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

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 ---
<ul>
<% for product in @products %>
  <li>
    <%= product.created_at %>
  </li>
<% end %>
</ul>

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

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())

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?

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

View ---
<ul>
<% for product in @products %>
  <li>
    <%= product.created_at %>
  </li>
<% end %>
</ul>

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

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

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.