Searching fields on has many associations returns duplicate results
Created by: tjvc
What were you trying to do?
Mark a has many field as searchable
and set some searchable_fields
. Perform a search, and the parent record will appear multiple times on the dashboard (when it has more than one associated child).
I'm not sure if this is intended to be supported (the search options don't seem to be documented for a has many field), but it at least partially works.
What did you end up with (logs, or, even better, example apps are great!)?
Here's an example query from the logs (this is a has many through association, but the principle is the same):
SELECT "users".* FROM "users" LEFT OUTER JOIN "committee_customers" ON "committee_customers"."user_id" = "users"."id" LEFT OUTER JOIN "committees" ON "committees"."id" = "committee_customers"."committee_id" WHERE "users"."role" = $1 AND "users"."soft_deleted" = $2 AND (LOWER(CAST("users"."email" AS CHAR(256))) LIKE '%test%' OR LOWER(CAST("users"."first_name" AS CHAR(256))) LIKE '%test%' OR LOWER(CAST("users"."last_name" AS CHAR(256))) LIKE '%test%' OR LOWER(CAST("committees"."name" AS CHAR(256))) LIKE '%test%') LIMIT $3 OFFSET $4
We're left joining associations, so I think all that's required is a DISTINCT
.
What versions are you running?
rails (6.0.4.1)
administrate (0.16.0)