FIX: Don't use joins to filter (#24904)

Posts may have multiple uploads/upload references.
This commit is contained in:
Daniel Waterworth 2024-01-10 23:11:29 -06:00 committed by GitHub
parent cabbc3899f
commit 30bea5c7c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -89,17 +89,17 @@ class TopicUploadSecurityManager
private
def posts_owning_uploads
Post.where(topic_id: @topic.id).joins("INNER JOIN uploads ON access_control_post_id = posts.id")
Post.where(topic_id: @topic.id, id: Upload.select(:access_control_post_id))
end
def posts_with_unowned_uploads
Post
.where(topic_id: @topic.id)
.joins(
"INNER JOIN upload_references ON upload_references.target_type = 'Post' AND upload_references.target_id = posts.id",
)
.joins("INNER JOIN uploads ON upload_references.upload_id = uploads.id")
.where("uploads.access_control_post_id IS NULL")
.includes(:uploads)
Post.where(
topic_id: @topic.id,
id:
UploadReference.where(
target_type: "Post",
upload: Upload.where(access_control_post_id: nil),
).select(:target_id),
).includes(:uploads)
end
end