diff --git a/app/models/post.rb b/app/models/post.rb index 9919729c3d0..f37a9cf1510 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -344,23 +344,22 @@ class Post < ActiveRecord::Base order('sort_order desc, post_number desc') end - def self.summary(topic_id = nil) - # PERF: if you pass in nil it is WAY slower - # pg chokes getting a reasonable plan - topic_id = topic_id ? topic_id.to_i : "posts.topic_id" + def self.summary(topic_id) + topic_id = topic_id.to_i # percent rank has tons of ties - where(["post_number = 1 or id in ( - SELECT p1.id - FROM posts p1 - WHERE p1.percent_rank <= ? AND - p1.topic_id = #{topic_id} - ORDER BY p1.percent_rank - LIMIT ? - )", - SiteSetting.summary_percent_filter.to_f / 100.0, - SiteSetting.summary_max_results - ]) + where(topic_id: topic_id) + .where(["(post_number = 1) or id in ( + SELECT p1.id + FROM posts p1 + WHERE p1.percent_rank <= ? AND + p1.topic_id = #{topic_id} + ORDER BY p1.percent_rank + LIMIT ? + )", + SiteSetting.summary_percent_filter.to_f / 100.0, + SiteSetting.summary_max_results + ]) end def update_flagged_posts_count diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 78c8d237936..9c89c807dda 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -805,12 +805,13 @@ describe Post do let!(:p1) { Fabricate(:post, post_args.merge(score: 4, percent_rank: 0.33)) } let!(:p2) { Fabricate(:post, post_args.merge(score: 10, percent_rank: 0.66)) } let!(:p3) { Fabricate(:post, post_args.merge(score: 5, percent_rank: 0.99)) } + let!(:p4) { Fabricate(:post, percent_rank: 0.99) } it "returns the OP and posts above the threshold in summary mode" do SiteSetting.summary_percent_filter = 66 - expect(Post.summary.order(:post_number)).to eq([p1, p2]) + expect(Post.summary(topic.id).order(:post_number)).to eq([p1, p2]) + expect(Post.summary(p4.topic.id)).to eq([p4]) end - end context 'sort_order' do