Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Issues
  • #2147
Closed
Open
Issue created Feb 21, 2022 by Ruan Carlos@ruanltbg

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 searchable Field::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
Assignee
Assign to
Time tracking