问题
I'm using Rails 3.2.13, ruby 1.9.3 and Oracle 11g. When saving a record in the DB with a character like 'ñ' I get the error below:
ActiveRecord::StatementInvalid (Encoding::UndefinedConversionError:
U+00F1 from UTF-8 to US-ASCII: INSERT INTO "OMNIAUTH_USERS" ("CREATED_AT",
"FIRST_NAME", "ID", "LAST_NAME", "STATUS", "UID", "UPDATED_AT") VALUES (:a1,
:a2, :a3, :a4, :a5, :a6, :a7)):
app/controllers/user_sessions_controller.rb:18:in `create'
I tried to run this query to see the language used by Oracle:
SELECT USERENV ('language') FROM DUAL
It returned AMERICAN_AMERICA.AL32UTF8.
These are my gems for Oracle:
gem 'ruby-oci8', '~> 2.1.5'
gem 'activerecord-oracle_enhanced-adapter', '~> 1.4.2'
gem 'ruby-plsql', '~> 0.5.0'
What should I do?
回答1:
You have to distinguish two different nls settings
the external one - defined by the environment variable NLS_LANG in you application. This determines your internal string representation when you send data to OCI client library.
the internal one. It is the character set used by Oracle to store your data on disk.
Try to execute
select r.module, t.*
from v$sesssion_connection_info t
join v$session r on (r.sid = t.sid and t.serial# = r.serial#)
where r.sid = <your ruby connection SID>;
select * from nls_database_parameters;
select * from nls_instance_parameters;
If it shows, that you are using something like US7ASCII or ISO8859P1 then Oracle accepts your character and converts it into target character set(either by removing an accent or by replacing with '?').
来源:https://stackoverflow.com/questions/18071327/rails3-cannot-save-%c3%b1-to-oracle-11g