pools/show: fix N+1 query on pool show page.

Fix a N+1 query when fetching posts to render thumbnails. Also adds
support for the `limit` url param on the posts show page.
This commit is contained in:
evazion
2019-08-08 23:12:40 -05:00
parent 6d171f44c4
commit 4f024d2360
4 changed files with 14 additions and 18 deletions

View File

@@ -433,6 +433,7 @@ class PostQueryBuilder
if q[:ordpool].present?
pool_id = q[:ordpool].to_i
# XXX unify with Pool#posts
pool_posts = Pool.joins("CROSS JOIN unnest(pools.post_ids) WITH ORDINALITY AS row(post_id, pool_index)").where(id: pool_id).select(:post_id, :pool_index)
relation = relation.joins("JOIN (#{pool_posts.to_sql}) pool_posts ON pool_posts.post_id = posts.id").order("pool_posts.pool_index ASC")
end