daveomcd
daveomcd

Reputation: 6555

Rails: Error when trying to save a special character to database when using ActiveRecord and TinyTDS

I'm trying to save this character "É" as part of a string using ActiveRecord to my sql server using TinyTDS. Any idea on what I need to do to get rid of the following error?

Exiting
/var/lib/gems/1.9.1/gems/activerecord-sqlserver-adapter-3.2.10/lib/active_record/connection_adapters/sqlserver/database_statements.rb:387:in `execute': TinyTds::Error: Error converting characters into server's character set. Some character(s) could not be converted

The string I imagine it's erroring out on is the following: JEREMÉ

Below is my code I'm using to parse the file...

csv = CSV.parse(csv_text, :headers=>false, encoding: "UTF-8")
csv.drop(2).each do |row|
    person = Person.new(:lastname => row[0], :firstname => row[1])
    playerstats.save
end

Upvotes: 3

Views: 1455

Answers (1)

baash05
baash05

Reputation: 4516

value = value.force_encoding('ISO-8859-1').encode('UTF-8')

This will make your string database happy.

Upvotes: 5

Related Questions