mirror of
https://github.com/discourse/discourse.git
synced 2025-03-29 13:46:40 +08:00

Calling pluck is instantly making a SELECT, while passing the relationship allows rails to build a correct query. Before (2 selects): ``` pry(main)> Post.where(topic_id: Topic.where(id: [1,3,4]).pluck(:id)).count (1.3ms) SELECT "topics"."id" FROM "topics" WHERE "topics"."deleted_at" IS NULL AND "topics"."id" IN (1, 3, 4) Post Count (0.5ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."deleted_at" IS NULL AND "posts"."topic_id" IN (1, 3, 4) ``` After (1 select): ``` pry(main)> Post.where(topic_id: Topic.where(id: [1,3,4])).count Post Count (2.7ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."deleted_at" IS NULL AND "posts"."topic_id" IN (SELECT "topics"."id" FROM "topics" WHERE "topics"."deleted_at" IS NULL AND "topics"."id" IN (1, 3, 4)) ```