Find all in datetime with only date

Tagged with Rails dates

Language: Ruby

When you store your dates as DateTimes, but are given only a date, use this named scope to select all items on a given date. Could probably be written to accept multiple dates as well.

View as text

named_scope :for_date, lambda { |date|
  from = DateTime.parse "#{date} 12:00 AM"
  to = DateTime.parse "#{date} 11:59:59 PM"
    :conditions => ["datetime BETWEEN ? AND ?", from, to]

#sample use
tours = Tour.for_date(
Original snippet written by Aaron Godin
Last updated at 13:32 PM on Feb 02, 2011

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.