Fixtures: How to load utf-8 characters and convert to non utf-8 and save into database?

匆匆过客 提交于 2020-01-06 01:16:07

问题


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).


回答1:


I solved this by

  1. add the following line in boot.rb

ENV['NLS_LANG'] ||= 'AMERICAN_AMERICA.JA16SJIS'

  1. 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).


来源:https://stackoverflow.com/questions/36492113/fixtures-how-to-load-utf-8-characters-and-convert-to-non-utf-8-and-save-into-da

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!