title: Ordering Records with Same Date Using Second Field
body: <p>I have an application with many Galleries. Each gallery has a start_date DateTime field.</p>
  
  <p>For legacy reasons, the time of all start_dates is midnight.</p>
  
  <pre><code>Thu, 10 Jul 2014 00:00:00 UTC +00:00
  </code></pre>
  
  <p>I need to order the galleries by date, so a user can shuffle back and forth across them using a 'older' and 'newer' link. Galleries are ordered based on start_date:</p>
  
  <pre><code>scope :start_date_ascending, -&gt; { order(start_date: :asc) }
  scope :start_date_descending, -&gt; { order(start_date: :desc) }
  </code></pre>
  
  <p>My problem is that there is no clear older or newer gallery when there are multiple galleries with the same date. In such cases I cannot predict the order that Galleries with the same date are returned in, so moving across multiple galleries with the same date becomes random and error-prone.</p>
  
  <p>I have scope set up to find newer and older galleries:</p>
  
  <pre><code>scope :newer_than, -&gt; (gallery){ where.not(id:gallery).where('start_date &gt;= :gallery_start_date', gallery_start_date:gallery.start_date) }
  scope :older_than, -&gt; (gallery){ where.not(id:gallery).where('start_date &lt; :gallery_start_date', gallery_start_date:gallery.start_date) }
  </code></pre>
  
  <p>And I find the next and previous galleries like this:</p>
  
  <pre><code>def self.next_newer(gallery)
    Gallery.newer_than(gallery).start_date_ascending.limit(1).first
  end
  
  def self.next_older(gallery)
    Gallery.older_than(gallery).start_date_descending.limit(1).first
  end
  </code></pre>
  
  <p>So it seems to me I need a secondary way to order the galleries that are the same date. It doesn't matter what order that is - it could simply be their ids.</p>
  
  <p>How can I handle this situation so that galleries with the same date appear in the query in a predictable, fixed order so that <code>next_newer</code> and <code>next_older</code> move through them?</p>