Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A self referential belongs_to creates an incorrect SQL query #2249

Open
mackross opened this issue Aug 17, 2022 · 1 comment
Open

A self referential belongs_to creates an incorrect SQL query #2249

mackross opened this issue Aug 17, 2022 · 1 comment
Labels
bug breakages in functionality that is implemented search finding things through our models

Comments

@mackross
Copy link

mackross commented Aug 17, 2022

  • 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
@mackross mackross added the bug breakages in functionality that is implemented label Aug 17, 2022
@pablobm
Copy link
Collaborator

pablobm commented Dec 25, 2022

@mackross - Thank you for this report. It's indeed a bug but unfortunately it doesn't (I think) have an easy fix. It'll have to be part of an effort to redesign the search, which is one of the things in my radar.

A workaround would be to implement your own search, as explained at https://github.com/pablobm/administrate/blob/howto-search/docs/guides/customising_search.md (those docs are not published officially yet, but will work).

@pablobm pablobm added the search finding things through our models label Dec 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug breakages in functionality that is implemented search finding things through our models
Projects
None yet
Development

No branches or pull requests

2 participants