装置:如何加载utf-8字符并转换为非utf-8并保存到数据库中? [英] Fixtures: How to load utf-8 characters and convert to non utf-8 and save into database?
问题描述
我正在处理依赖于Fixtures的旧版代码,将非UTF8字符从yml文件加载到数据库中。
使用rails最新版本,yml必须是utf-8编码,这意味着Fixture读取的是utf-8编码。
如何转换以下代码,以便将其从utf-8转换为非utf8并加载到数据库(当然是非utf8编码)。
class LoadUsersData< ActiveRecord :: Migration
def self.up
down
directory = File.join(File.dirname(__ FILE__))
Fixtures.create_fixtures(目录,用户)
end
def self.down
User.delete_all
end
end
其中users.yml包含utf-8格式的数据(必须)。
或者可以使用.txt (可以是非utf8格式)并使用Fixtures加载?
我正在使用oracle_enhanced,我试图将encoding更改为database.yml中的数据库编码,但是似乎没有从utf-8到我的编码自动完成转换(因为utf-8的这两个编码的字符串长度不同,所以数据库返回的是user_name太长的错误) / p>
我通过
- 在boot.rb中添加以下行
ENV ['NLS_LANG'] || ='AMERICAN_AMERIC A.JA16SJIS'
- 将所有yml文件转换为utf-8格式(请注意,使用sjis格式的yml和ruby gem syck将加载yml,但是夹具不会工作,即使加载了yml)。
I am new to Rails.
I am working on a legacy code which relies on Fixtures to load non UTF8 characters from yml files into database.
With rails lastest version, the yml must be utf-8 encoding, meaning that what Fixtures read in will be utf-8 encoding.
How to convert the following codes so that it will convert from utf-8 to non-utf8 and load into database (non-utf8 encoding of course).
class LoadUsersData < ActiveRecord::Migration
def self.up
down
directory = File.join(File.dirname(__FILE__))
Fixtures.create_fixtures(directory, "users")
end
def self.down
User.delete_all
end
end
where users.yml contains data in utf-8 format (have to).
Or is it possible to use .txt (can be in non-utf8 format) and load with Fixtures?
I am using oracle_enhanced, and I tried to change "encoding" to my database encoding in database.yml, but it seems that the conversion is not done automatically from utf-8 to my encoding (because the string length is different for these two encodings with utf-8 has more bytes, and the database returns that user_name is too long error).
I solved this by
- add the following line in boot.rb
ENV['NLS_LANG'] ||= 'AMERICAN_AMERICA.JA16SJIS'
- convert all yml files to utf-8 format (Note that uses sjis formatted yml and ruby gem syck will load the yml but fixtures wont work even yml is loaded).
这篇关于装置:如何加载utf-8字符并转换为非utf-8并保存到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!