perf: Story.has_suggestions?

This commit is contained in:
Peter Bhat Harkins 2020-08-28 08:59:20 -05:00
parent dd2e74f63a
commit 823a147ad0
1 changed files with 7 additions and 1 deletions

View File

@ -480,7 +480,13 @@ class Story < ApplicationRecord
end
def has_suggestions?
self.suggested_taggings.any? || self.suggested_titles.any?
# perf: save the round trip on a second query, equivalent to the below:
# self.suggested_taggings.any? || self.suggested_titles.any?
Story.joins(:suggested_taggings, :suggested_titles)
.where('stories.id = ? and
(suggested_taggings.id is not null or suggested_titles.id is not null)
', self.id)
.exists?
end
def hider_count