Fix #5312: StatementInvalid Error When Querying the media_assets Attribute of Uploads

This commit is contained in:
evazion
2022-10-25 14:15:47 -05:00
parent c96d60a840
commit 6413b9abcd
5 changed files with 71 additions and 8 deletions

View File

@@ -643,15 +643,44 @@ module Searchable
end
if parameter_hash?(params[attr])
if association.belongs_to?
if association.through_reflection?
source_association = association.source_reflection
through_association = association.through_reflection
if source_association.macro == :belongs_to
source_foreign_key = source_association.foreign_key
source_primary_key = source_association.association_primary_key
elsif source_association.macro == :has_one
source_foreign_key = source_association.join_foreign_key
source_primary_key = source_association.join_primary_key
else
source_foreign_key = source_association.association_primary_key
source_primary_key = source_association.foreign_key
end
if through_association.macro == :belongs_to
through_foreign_key = through_association.foreign_key
through_primary_key = through_association.association_primary_key
elsif through_association.macro == :has_one
through_foreign_key = through_association.join_foreign_key
through_primary_key = through_association.join_primary_key
else
through_foreign_key = through_association.association_primary_key
through_primary_key = through_association.foreign_key
end
source_subquery = source_association.klass.visible(current_user).search(params[attr], current_user).reorder(nil)
through_subquery = through_association.klass.visible(current_user).where(source_foreign_key => source_subquery.select(source_primary_key))
relation = visible(relation, attr).where(through_foreign_key => through_subquery.select(through_primary_key))
elsif association.belongs_to?
foreign_key = association.foreign_key
primary_key = association.association_primary_key
relation = visible(relation, attr).where(foreign_key => model.visible(current_user).search(params[attr], current_user).reorder(nil).select(primary_key))
else
foreign_key = association.association_primary_key
primary_key = association.foreign_key
relation = visible(relation, attr).where(foreign_key => model.visible(current_user).search(params[attr], current_user).reorder(nil).select(primary_key))
end
relation = visible(relation, attr).where(foreign_key => model.visible(current_user).search(params[attr], current_user).reorder(nil).select(primary_key))
end
relation

View File

@@ -1412,7 +1412,7 @@ class Post < ApplicationRecord
:last_comment_bumped_at, :last_commented_at, :last_noted_at,
:uploader, :approver, :parent,
:artist_commentary, :flags, :appeals, :notes, :comments, :children,
:approvals, :replacements],
:approvals, :replacements, :media_metadata],
current_user: current_user
)