Formatting dates for SQL queries in Rails

Here’s a simple and clean way to format date for use in SQL query in Ruby On Rails.

User.find :all, :conditions => ['created_at > ?', 7.days.ago.to_formatted_s(:db)]

You can use to_formatted_s(format) method with different data types – Date, Time, DateTime, Range, Array and BigDecimal. There are also more formats available apart from :db. Find out more in Rails API.