From 514b736a07cd8671d03a251e4937202c3a939c2b Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 27 Dec 2019 12:53:46 -0600 Subject: [PATCH] pools: fix crash in post_tags_match search option. Fix exception in /pools/search[post_tags_match]=touhou. Caused by `Pool.post_tags_match(query).count` generating invalid SQL (`count` doesn't like the top-level cross join). ref: https://danbooru.donmai.us/forum_topics/9127?page=289#forum_post_161821 --- app/models/pool.rb | 3 ++- test/unit/pool_test.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/pool.rb b/app/models/pool.rb index c00dc2523..61a557521 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -51,7 +51,8 @@ class Pool < ApplicationRecord def post_tags_match(query) posts = Post.tag_match(query).select(:id).reorder(nil) - joins("CROSS JOIN unnest(post_ids) AS post_id").group(:id).where("post_id IN (?)", posts) + pools = Pool.joins("CROSS JOIN unnest(post_ids) AS post_id").group(:id).where("post_id IN (?)", posts) + where(id: pools) end def default_order diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index 5710439fa..b5f5a2cac 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -87,6 +87,10 @@ class PoolTest < ActiveSupport::TestCase assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "bkub").pluck(:id)) assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "fumimi").pluck(:id)) assert_equal([@pool2.id], Pool.search(post_tags_match: "bkub fumimi").pluck(:id)) + + assert_equal(2, Pool.search(post_tags_match: "bkub").count) + assert_equal(2, Pool.search(post_tags_match: "fumimi").count) + assert_equal(1, Pool.search(post_tags_match: "bkub fumimi").count) end end