title: Added Next/Previous buttons to Rails view. Why is Next skipping some instances?
body: <p>I've added Next and Previous buttons to cycle through views of a resource on a Rails 3.2 app. Next and Previous should just cycle through the ID.</p>
  
  <p>The Previous button works as expected: it's a link to (the current ID minus 1).</p>
  
  <p>However, the Next button links to arbitrary ID numbers. For any ID less than 17, the Next button links to ID number 17. Clicking that shows a Next button linking to 31, then 45, then 58, 59, 65, 82, 108, 115, 120, 127, 128, 129, 131, ... From there, gaps are less frequent but still do appear.</p>
  
  <p>The resource is called Lecture, so here's the relevant part of app/models/lecture.rb:</p>
  
  <pre><code>def previous
    Lecture.where(["id &lt; ?", id]).last
  end
  
  def next
    Lecture.where(["id &gt; ?", id]).first
  end
  </code></pre>
  
  <p>And here's the view, app/views/lectures/show.html.erb:</p>
  
  <pre><code>&lt;li class="previous"&gt;
    &lt;%= link_to "Previous", @lecture.previous if @lecture.previous.present? %&gt;
  &lt;/li&gt;
  &lt;li class="next"&gt;
    &lt;%= link_to "Next", @lecture.next if @lecture.next.present? %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p><strong>The production data for the Resource lecture actually has continuous IDs all the way from 1 to 173.</strong></p>
  
  <p>I suspect this is an issue related to batch-editing my production data, but I can think of no way to be sure. I'm on Heroku/postgres.</p>