diff --git a/lib/topic_view.rb b/lib/topic_view.rb index a53fc0f4080..657c985b1f0 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -416,10 +416,10 @@ class TopicView end def closest_post_to(post_number) - closest_posts = filter_post_ids_by("@(post_number - #{post_number})") - return nil if closest_posts.empty? + closest_post = @filtered_posts.order("@(post_number - #{post_number})").limit(1).pluck(:id) + return nil if closest_post.empty? - filtered_post_ids.index(closest_posts.first) || filtered_post_ids[0] + filtered_post_ids.index(closest_post.first) || filtered_post_ids[0] end end