Fix #4869: "Random" button raises exception when viewing ordfav.

Fix exception during https://danbooru.donmai.us/posts/random?tags=ordfav:nonamethanks

Before we were doing a query like this:

    SELECT
      "posts".*
    FROM
      "posts"
    INNER JOIN
      "favorites" ON "favorites"."post_id" = "posts"."id"
    WHERE
      (favorites.user_id % 100 = 64 AND favorites.user_id = 52664)
      AND "posts"."id" = 343894
    ORDER BY
      favorites.id DESC,
      posts.id DESC,
      ID=343894 DESC

but `ID=? DESC` is ambiguous during an ordfav: search because of the
join on the favorites table. The fix is to qualify the reference as
`posts.id`.
This commit is contained in:
evazion
2021-08-30 16:44:40 -05:00
parent 1e5c7d6f0f
commit 49d18e64e8
2 changed files with 8 additions and 1 deletions

View File

@@ -505,7 +505,7 @@ module Searchable
def find_ordered(ids)
order_clause = []
ids.each do |id|
order_clause << sanitize_sql_array(["ID=? DESC", id])
order_clause << sanitize_sql_array(["#{qualified_column_for(:id)} = ? DESC", id])
end
where(id: ids).order(Arel.sql(order_clause.join(', ')))
end

View File

@@ -492,6 +492,13 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to(post_path(@post, tags: "aaaa"))
end
should "render for a ordfav: search" do
@post = as(@user) { create(:post, tag_string: "fav:me") }
get random_posts_path, params: { tags: "ordfav:#{@user.name}" }
assert_redirected_to(post_path(@post, tags: "ordfav:#{@user.name}"))
end
should "return a 404 when no random posts can be found" do
get random_posts_path, params: { tags: "qoigjegoi" }
assert_response 404