Joe*_*lio 10 ruby mysql ruby-on-rails character-encoding
Rails 2.3.5和Ruby 1.8.7和Mysql 5.1.53
我在一个csv文件中加载,它有一个TM符号在其中的字段(商标)
Tart Deco™ - 看起来像这样
我正在尝试做一个有效的记录查找:
Influencer.find(:first,:conditions => ["author_name =?and url_discovered =?",author_name,site_profile_url])
Mysql ::错误:操作'='非法混合排序(latin1_swedish_ci,IMPLICIT)和(utf8_general_ci,COERCIBLE):SELECT*FROM influencersWHERE(author_name ='Tart Deco?'和url_discovered ='http://www.joelnylund. com')限制1
在ruby调试器中,String显示为:
p author_name"Tart Deco\231"
我的表格编码为"utf8_general_ci"
所以我该怎么做?如果我存储TM,我真的不在乎,它会很好,主要是我不想让它破坏...
fx_*_*fx_ 16
确保您的表格具有uft8字符集:
alter table `influencers` convert to character set utf8 collate utf8_general_ci;
Run Code Online (Sandbox Code Playgroud)
注意:您的排序规则(utf8_general_ci)不是您的编码(字符集) - 与MySQL的常见误解.
也许不是最有帮助的答案,但我只是在 Rails 3 w/ Ruby 1.9.2 中组合了一个示例测试,它工作得很好。据我所知,字符编码在 Ruby 1.9 中进行了重大修改。
\n\n注意:在我的测试中,我只是复制了您的文本并在 Rails 控制台中
\n\n:001 > author_name = \'Tart Deco\xe2\x84\xa2\'\n=> "Tart Deco\xe2\x84\xa2"\n:002 > Influencer.find(:first,:conditions => ["author_name = ?", author_name])\n=> nil \nRun Code Online (Sandbox Code Playgroud)\n\n当然,我只是捏造了一个没有记录的影响者模型。但 MySQL 并没有呕吐。因此,我在模型中添加了一条带有该名称的记录,并且检索正常。
\n\n华泰
\n