Latest posts API pagination

Okay, now that I’ve looked at the controller for /posts:

def latest
  params.permit(:before)
  ...

there is a “before” parameter, not a “page” parameter, but using it like a “page” is a bit different because it filters out private posts and such. But I think roughly you can use it like this:

  • hit /posts.json and get the first post id (currently it is 22000)
  • subtract 50 (that endpoint only shows at most 50 at a time)
  • hit /posts.json?before=21950
  • repeat

Part of the issue which doesn’t make total sense to me is that we are limiting the sql query to 50 results, but then we do more filtering on it after the initial sql query which is why you will not always get 50 results.

4 Likes