faded picture of luke
a semi-random photo | click for the full photo gallery
click to browse photos
homepage navigation

Luke Melia

Formatting a date in a SQL query in Rails

If you need to use a date/time in a SQL fragment in Ruby on Rails you can format it using the to_formatted_s method.

    num_entries_in_last_24_hours = Entry.count(:conditions => "created_at < '#{24.hours.ago.to_formatted_s(:db)}'")

This works for mysql, not sure about other databases. Here’s the relevant API link.

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!
Luke Melia on software development freelance web development how to contact me Luke Melia, Software Developer letters and more from my travels photo gallery personal philosophy