0

On rails 6 with postgres on heroku, I am looking for a simple method to perform a search with or without latin character.

For example: the user enters "gérard" and the query should find all result with "gérard" ou "gèrard"

or

the user enters "gerard" and the query should return result with "gérard", ...

 Invoices.where("withoutlatincharacter(name) like ?", "%#{"gérard"}%"))

What should I put instead of withoutlatincharacter?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Denis Bolomier
  • 350
  • 2
  • 19
  • You can replace the characters in Rails with [`transliterate`](https://stackoverflow.com/a/58601778/2622934). You may want to create a column where you use `transliterate` to normalize the data and always save the values without the accented characters. Or you could run 2 queries, once with the accents and once without. – cschroed Apr 02 '21 at 20:57

1 Answers1

0

One way is to use unaccent extension in PostgreSQL

first install the module :

CREATE EXTENSION unaccent;

then you are good to go , pass your string as it is in ruby and change your sql query to this :

... where unaccent(name) like '%' || unaccent('gérard') || '%
eshirvana
  • 23,227
  • 3
  • 22
  • 38