From 7e99be0b2eb878beb9f35c0b1acfd24f42c00265 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 25 Oct 2022 23:30:57 -0500 Subject: [PATCH] searchable: better fix for #5312. Simplify fix for #5312. Fixes an exception when searching `has_one` associations like `/wiki_pages?search[artist][is_banned]=true`. --- app/logical/concerns/searchable.rb | 34 +++--------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/app/logical/concerns/searchable.rb b/app/logical/concerns/searchable.rb index 3bf5a80aa..3612b1b64 100644 --- a/app/logical/concerns/searchable.rb +++ b/app/logical/concerns/searchable.rb @@ -647,39 +647,11 @@ module Searchable 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)) + through_subquery = through_association.klass.visible(current_user).where(attr => source_subquery) + relation = visible(relation, attr).where(through_association.name => through_subquery) 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)) + relation = visible(relation, attr).where(attr => model.visible(current_user).search(params[attr], current_user).reorder(nil)) end end