Skip to content

A platform for community discussion. Free, open, simple.

DEV: Prefer nested queries (#23464)

Bianca Nenciu3700514819d3
diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb
index ba5d59879e1..002f2e52b06 100644
--- a/app/controllers/about_controller.rb
+++ b/app/controllers/about_controller.rb
@@ -20,7 +20,7 @@ class AboutController < ApplicationController
unless current_user.staff?
RateLimiter.new(current_user, "live_post_counts", 1, 10.minutes).performed!
end
- category_topic_ids = Category.pluck(:topic_id).compact!
+ category_topic_ids = Category.select(:topic_id).where.not(topic_id: nil)
public_topics =
Topic.listable_topics.visible.secured(Guardian.new(nil)).where.not(id: category_topic_ids)
stats = { public_topic_count: public_topics.count }
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 4b8b060dc72..92bdf13da01 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -931,7 +931,7 @@ class ApplicationController < ActionController::Base
Discourse
.cache
.fetch(key, expires_in: 10.minutes) do
- category_topic_ids = Category.pluck(:topic_id).compact
+ category_topic_ids = Category.select(:topic_id).where.not(topic_id: nil)
@top_viewed =
TopicQuery
.new(nil, except_topic_ids: category_topic_ids)
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 06eb231ff46..1a0588dd689 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -344,7 +344,7 @@ class TopicsController < ApplicationController
topic = Topic.find_by(id: params[:id])
guardian.ensure_can_edit!(topic)
- category = Category.where(id: params[:category_id].to_i).first
+ category = Category.find_by(id: params[:category_id].to_i)
guardian.ensure_can_publish_topic!(topic, category)
row_count = SharedDraft.where(topic_id: topic.id).update_all(category_id: category.id)
diff --git a/app/models/category_featured_topic.rb b/app/models/category_featured_topic.rb
index 1602dc7ad09..5192f3f4a6a 100644
--- a/app/models/category_featured_topic.rb
+++ b/app/models/category_featured_topic.rb
@@ -29,8 +29,7 @@ class CategoryFeaturedTopic < ActiveRecord::Base
if batched
if categories.length == batch_size
- next_id =
- Category.where("id > ?", categories.last.id).order("id asc").limit(1).pluck(:id)[0]
+ next_id = Category.where("id > ?", categories.last.id).order(:id).pick(:id)
next_id ? Discourse.redis.setex(NEXT_CATEGORY_ID_KEY, 1.day, next_id) : clear_batch!
else
clear_batch!
diff --git a/app/models/topic.rb b/app/models/topic.rb
index e2c6434c2ba..4ed8755aa65 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -569,8 +569,7 @@ class Topic < ActiveRecord::Base
topics = topics.limit(opts[:limit]) if opts[:limit]
# Remove category topics
- category_topic_ids = Category.pluck(:topic_id).compact!
- topics = topics.where("topics.id NOT IN (?)", category_topic_ids) if category_topic_ids.present?
+ topics = topics.where.not(id: Category.select(:topic_id).where.not(topic_id: nil))
# Remove muted and shared draft categories
remove_category_ids =