Ruby on Rails: compare two strings in terms of database mapping

I have a list of words and you want to find which ones already exist in the database.

Instead of doing dozens of SQL queries, I decided to use "SELECT wordFROM tableWHERE wordIN (array_of_words)" and then execute the result.

The problem is sorting the database.

http://www.collation-charts.org/mysql60/mysql604.utf8_general_ci.european.html

There are many different characters that MySQL treats as one and the same. However, in Ruby, string1 will not equal string2.

For example: if the word "šuo", the database can also return "suo" if it is found (and this is normal), but when I want to check if something is found "šuo", Ruby, of course, returns false (šuo ! = suo).

So, is there a way to compare two strings in Ruby in terms of the same sorting?

+3
source share
1 answer

I used iconv like this for something like this:

require 'iconv'

class String
  def to_ascii_iconv
    Iconv.new('ASCII//IGNORE//TRANSLIT', 'UTF-8').iconv(self).unpack('U*').select { |cp| cp < 127 }.pack('U*')
  end
end

puts 'suo'.to_ascii_iconv
# => suo
puts 'šuo'.to_ascii_iconv
# => suo
puts 'suo'.to_ascii_iconv == 'šuo'.to_ascii_iconv
# => true

Hope this helps!

Zubin

0
source

All Articles