Simple range question

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.

On 6/21/06, Nathan P. Verni [email protected] wrote:

: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 L. wrote:

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 G.
grzm seespotcode net

On 6/21/06, Michael G. [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs