I have a model with a datetime field called start_date. I''d like to do a find on the model so that it returns only records with the start_date equal to the current day. Something like this: @foos = Foo.find(:all, :conditions => //?? start_date equal to today''s date ??// ] ) Thanks in advance for any help. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060621/d6f68443/attachment.html
On 6/21/06, Nathan P. Verni <nverni@blenderbox.com> wrote:> I have a model with a datetime field called start_date. I''d like to do a > find on the model so that it returns only records with the start_date equal > to the current day. > > Something like this: > > @foos = Foo.find(:all, > > :conditions => //?? start_date equal to today''s date ??// ] > ) >:conditions => ["start_date like ?", "#{Date.today.to_s}%"] Caveat: I''m not entirely sure that this is 100% database agnostic. It relies on the fact that the underlying data looks like "yyyy-mm-dd hh:mm:ss". It works on Postgres anyway.
On Jun 22, 2006, at 3:12 , James Ludlow wrote:> On 6/21/06, Nathan P. Verni <nverni@blenderbox.com> wrote: >> I have a model with a datetime field called start_date. I''d like >> to do a >> find on the model so that it returns only records with the >> start_date equal >> to the current day. > > :conditions => ["start_date like ?", "#{Date.today.to_s}%"] > > Caveat: I''m not entirely sure that this is 100% database agnostic. It > relies on the fact that the underlying data looks like "yyyy-mm-dd > hh:mm:ss". It works on Postgres anyway.You''re probably safer using SQL-spec CURRENT_DATE or CURRENT_TIMESTAMP (which PostgreSQL also supports). And I''m not sure why you''re using LIKE. I''d just use a straight-up =. :conditions => ''start_date = CURRENT_DATE'' If your start_date includes time which you wish to ignore (i.e., you want to return records with start_date ''2006-06-22 12:00'' and ''2006-06-22 13:00'' when you''re searching for today on 2006-06-22), you may want to use :conditions => ''CAST(start_date AS DATE) = CURRENT_DATE'' Hope this helps. Michael Glaesemann grzm seespotcode net
On 6/21/06, Michael Glaesemann <grzm@seespotcode.net> wrote:> On Jun 22, 2006, at 3:12 , James Ludlow wrote: > > > On 6/21/06, Nathan P. Verni <nverni@blenderbox.com> wrote: > >> I have a model with a datetime field called start_date. I''d like > >> to do a > >> find on the model so that it returns only records with the > >> start_date equal > >> to the current day. > > > > :conditions => ["start_date like ?", "#{Date.today.to_s}%"] > > > > Caveat: I''m not entirely sure that this is 100% database agnostic. It > > relies on the fact that the underlying data looks like "yyyy-mm-dd > > hh:mm:ss". It works on Postgres anyway. > > You''re probably safer using SQL-spec CURRENT_DATE or > CURRENT_TIMESTAMP (which PostgreSQL also supports). And I''m not sure > why you''re using LIKE. I''d just use a straight-up =. > > :conditions => ''start_date = CURRENT_DATE''Because he said that he had a datetime field.> If your start_date includes time which you wish to ignore (i.e., you > want to return records with start_date ''2006-06-22 12:00'' and > ''2006-06-22 13:00'' when you''re searching for today on 2006-06-22), > you may want to use > > :conditions => ''CAST(start_date AS DATE) = CURRENT_DATE''This is better than what I was doing. Thanks. -- James