From 8ec6d0ea6c1e62b9410bca4e6bdcc163543d00fc Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 30 Jul 2013 09:54:29 +1000 Subject: [PATCH] let's not run 3 queries, when a single query will do. --- lib/suggested_topics_builder.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/suggested_topics_builder.rb b/lib/suggested_topics_builder.rb index afcf42ffb..35ca744c0 100644 --- a/lib/suggested_topics_builder.rb +++ b/lib/suggested_topics_builder.rb @@ -12,17 +12,19 @@ class SuggestedTopicsBuilder def add_results(results) - return if results.blank? + # WARNING .blank? will execute an Active Record query + return unless results # Only add results if we don't have those topic ids already results = results.where('topics.id NOT IN (?)', @excluded_topic_ids) .where(closed: false, archived: false, visible: true) + .to_a - return if results.blank? - - # Keep track of the ids we've added - @excluded_topic_ids.concat results.map {|r| r.id} - @results.concat results + unless results.empty? + # Keep track of the ids we've added + @excluded_topic_ids.concat results.map {|r| r.id} + @results.concat results + end end def results_left @@ -37,4 +39,4 @@ class SuggestedTopicsBuilder @results.size end -end \ No newline at end of file +end