Allow searchable on multiple inherited classes
- What were you trying to do?
I have multiple classes that inherit from a
User
class and I want to use searchableField::BelongsTo
for those classes.
The problem:
When using the same table on multiple classes rails left_joins
creates an alias for the second join this way when filtering the data we should match against the alias and not the original table, I did not find any way to configure this alias on the administrate
config.
# model/user.rb
class User
end
# model/editor.rb
class Editor < User
end
# model/client.rb
class Client < User
end
# model/project.rb
class Project
belongs_to :editor, optional: true
belongs_to :client
end
# dashboard/project_dashboard.rb
class ProjectDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
client: Field::BelongsTo.with_options(
searchable: true,
searchable_fields: %w[name],
),
editor: Field::BelongsTo.with_options(
searchable: true,
searchable_fields: %w[name],
),
}
COLLECTION_ATTRIBUTES = %i[
client
editor
]
end
With this code I can filter by client name, but not by editor name. SQL Generated:
SELECT "projects".*
FROM "projects"
LEFT OUTER JOIN "users" ON "users"."id" = "projects"."client_id" AND "users"."type" IN ('Client')
LEFT OUTER JOIN "users" "editors_projects"
ON "editors_projects"."id" = "projects"."editor_id" AND "editors_projects"."type" IN ('Editor')
WHERE (LOWER(CAST("users"."name" AS CHAR(256))) LIKE '%test%' OR
LOWER(CAST("users"."name" AS CHAR(256))) LIKE '%test%')
Where it should be:
SELECT "projects".*
FROM "projects"
LEFT OUTER JOIN "users" ON "users"."id" = "projects"."client_id" AND "users"."type" IN ('Client')
LEFT OUTER JOIN "users" "editors_projects"
ON "editors_projects"."id" = "projects"."editor_id" AND "editors_projects"."type" IN ('Editor')
WHERE (LOWER(CAST("users"."name" AS CHAR(256))) LIKE '%test%' OR
LOWER(CAST("editors_projects"."name" AS CHAR(256))) LIKE '%test%')
Note the editors_projects
change on where.
It would be great if I could pass an alias
on the searchable configuration
- What versions are you running?
- Rails: 5.2.6
- administrate: Latest