FEATURE: Enable pagination of /posts.json

This commit is contained in:
riking 2015-01-23 21:22:19 -08:00
parent 1d24d8471e
commit 9e9119d1c1

View File

@ -26,8 +26,14 @@ class PostsController < ApplicationController
end
def latest
params.permit(:before)
last_post_id = params[:before].to_i
last_post_id = Post.last.id if last_post_id <= 0
# last 50 post IDs only, to avoid counting deleted posts in security check
posts = Post.order(created_at: :desc)
.where('posts.id > ?', Post.last.id - 50) # last 50 post IDs only, to avoid counting deleted posts in security check
.where('posts.id <= ?', last_post_id)
.where('posts.id > ?', last_post_id - 50)
.includes(topic: :category)
.includes(:user)
.limit(50)