From aae9e6e5fd0d47571fc9d0f4eaefe45e3fc6daa0 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 21 Aug 2020 10:51:37 +0800 Subject: [PATCH] FIX: `Topic.similar_to` results in error if cooked raw is blank. --- app/models/topic.rb | 12 +++++++----- spec/models/topic_spec.rb | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index e665125d250..9271cc6c3b9 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -597,12 +597,14 @@ class Topic < ActiveRecord::Base PrettyText.cook(raw[0...MAX_SIMILAR_BODY_LENGTH].strip) ) - raw_tsquery = Search.set_tsquery_weight_filter( - Search.prepare_data(cooked), - 'B' - ) + if cooked.present? + raw_tsquery = Search.set_tsquery_weight_filter( + Search.prepare_data(cooked), + 'B' + ) - tsquery = "#{tsquery} & #{raw_tsquery}" + tsquery = "#{tsquery} & #{raw_tsquery}" + end end tsquery = Search.to_tsquery(term: tsquery, joiner: "|") diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 516c50968e5..39d8a3bda2a 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -515,6 +515,10 @@ describe Topic do end end + it 'does not result in a syntax error when raw is blank after cooking' do + expect(Topic.similar_to('some title', '#')).to eq([]) + end + context 'with a similar topic' do fab!(:post) { SearchIndexer.enable