我有一个带有latin1
编码的遗留MySQL数据库,以及带有utf-8
编码的新MySQL数据库。当我将数据从旧数据库迁移到新数据库时,在网页中返回和呈现的字符串包含奇怪的字符。
例如:巧克力饼干
有一个模型连接到旧数据库以获取记录,并使用rake任务映射旧记录并创建新记录
class LegacyDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection(
adapter: "mysql2",
database: "old_database",
encoding: "latin1"
)
end
我提到this website来解决编码问题,但回退哈希是硬编码的。我需要一个动态的,这样每次调用修复编码的循环时我都不需要添加回退。
def fix_encoding(str)
# Referring to the solution from the website
new_str = str.encode('cp1252',:fallback => {
"\u0080" => "\x80".force_encoding("cp1252"),
"\u0081" => "\x81".force_encoding("cp1252"),
"\u008D" => "\x8D".force_encoding("cp1252"),
"\u008F" => "\x8F".force_encoding("cp1252"),
"\u0090" => "\x90".force_encoding("cp1252"),
"\u009D" => "\x9D".force_encoding("cp1252"),
"\u0098" => "\x98".force_encoding("cp1252"),
"\u0099" => "\x99".force_encoding("cp1252")
}).force_encoding("utf-8")
return new_str
end
我想将其更改为动态,但我无法转换它。
# How to do it in dynamic?
new_str = str.encode('cp1252', :fallback => Proc.new { |v| "#{v[4..5]}".force_encoding("cp1252") }).force_encoding("utf-8")
或者还有其他解决方案可以解决编码问题吗?