title: Rails get records after a certain ID
body: <p>In my app because records get created frequently I don't want to use a standard pagination mechanism because records wouldn't always be on the same page (records on page 2 today, might be on page 3 tomorrow). Instead what I want to do is request records after a certain ID.</p>
  
  <p>So let's say I have 100 records in my posts table ranging from IDs 1-100, and I show 10 records at a time.</p>
  
  <p>What I want to do is using <code>params[:after]</code> is request posts after that ID.</p>
  
  <p>So if I have <code>params[:after] == 10</code> I want to get records <code>11-21</code> (though 21 may not exist, so it's really 10 records in the table <code>created_at</code> ordered <code>desc</code> from the ID onwards.</p>
  
  <p>In my controller I have something like:</p>
  
  <pre><code>if params[:after].present?
    start_id = params[:after]
    end_id = start_id+10
    @posts = Post.where(:id =&gt; start_id..end_id).order('created_at desc').limit(10)
  else
    @posts = Post.order('created_at desc').limit(10)
  end
  </code></pre>
  
  <p>The problem I have is that the <code>end_id</code> will return the <code>start_id+10</code> but in the DB the next 10 records IDs might not be that, so I'd end up with incorrect IDs coming back.</p>
  
  <p>So ideally I need to know what the ID of the 10th post from this one is. How can I do this?</p>