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
  • #2249
Closed
Open
Issue created Aug 17, 2022 by Andrew Mackross@mackross

A self referential belongs_to creates an incorrect SQL query

  • What were you trying to do?

I've a user model that has a reference to itself as belongs_to :accountability_buddy, class_name: 'User' I want to use accountability_buddy: Field::BelongsTo.with_options(searchable: true, searchable_fields: 'email') and have the search return both a user and any users that this user is the accountability buddy for.

  • What did you end up with (logs, or, even better, example apps are great!)?

If I change the searchable fields instead to ['xxx'], the query demonstrates the issue.

21:32:54 web.1 | User Load (0.9ms) SELECT "users".* FROM "users" LEFT OUTER JOIN "users" "accountability_buddies_users" ON "accountability_buddies_users"."id" = "users"."accountability_buddy_id" WHERE (LOWER(CAST("users"."email" AS CHAR(256))) LIKE '%andrew@example.com%' OR LOWER(CAST("users"."xxx" AS CHAR(256))) LIKE '%andrew@example.com%' OR LOWER(CAST("users"."last_sign_in_ip" AS CHAR(256))) LIKE '%andrew@example.com%') LIMIT $1 OFFSET $2 [["LIMIT", 20], ["OFFSET", 0]]

You can see that it should the second OR statement should be as "accountability_buddies_users"."xxx" but it instead is "users"."xxx".

  • What versions are you running?
    • Rails 7
    • administrate 0.18
Assignee
Assign to
Time tracking