I’m having problems when connecting to a legacy database in SQL Server. The problem happens when read a string with special characters and try to insert it into another database (sqlite in development), which throws me the following error:
Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8
The database has a latin1-like collation. I have set client charset = UTF-8 in my freetds.conf file, and I have encoding: utf8 in my database.yml file.
It actually seems the freetds driver is making the right conversion, but for some reason the ruby library is not aware about it, because the problematic string is ESPA\xC3\x91A, which is actually the right binary representation for ESPAÑA, so calling force_encoding('UTF-8') on the string makes the error dissapear.
As an additional note, when I assign the string to the other model it gives me the following message:
Binary data inserted for `string` type on column `email`
Using https://github.com/rails-sqlserver/tiny_tds solved this and other problems I was having with the ODBC driver.