title: I need to find closest date in a rails app
body: <p>I need to find the closest date to a given date using a rails named scope.</p>
  
  <p>With MySql I could use DATEDIFF but I want to keep database agnosticism, following doesn't work with Sqlite:</p>
  
  <pre><code>named_scope :closest_to, lambda { |*args|
    date  = args.first
    count = args[1] || 1
    {:limit =&gt; count, :order =&gt; "ABS(DATEDIFF(hour, date_field, #{date.to_formatted_s :db}))"}
  }
  </code></pre>