Postgres accent insensitive LIKE search in Rails 3.1 on Heroku

后端 未结 5 2119
无人及你
无人及你 2021-02-05 11:11

How can I modify a where/like condition on a search query in Rails:

find(:all, :conditions => [\"lower(name) LIKE ?\", \"%#{search.downcase}%\"])

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 11:53

    Assuming Foo is the model you are searching against and name is the column. Combining Postgres translate and ActiveSupport's transliterate. You can do something like:

    Foo.where(
      "translate(
        LOWER(name),
        'âãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮ',
        'aaaaaaaaaaaaaaaeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuu'
      )
      LIKE ?", "%#{ActiveSupport::Inflector.transliterate("%qué%").downcase}%"
    )
    

提交回复
热议问题