Monday, July 17, 2006

Ruby on Rails, Unicode and Oracle

I was kind of in love with Ruby on Rails but now our relationship has hit the first bump: the infamous UTF-8 issue. In short, Ruby does not support UTF-8 (that's more or less the situation). So Rails does not either. However, my task at hand basically involves CRUD operations on a legacy (yet, UTF-8 enabled) Oracle db including German texts. Well, here's how to do that:

The first stop for Unicode in general is the wiki page on Unicode and Rails: http://wiki.rubyonrails.org/rails/pages/HowToUseUnicodeStrings

The problem with that page is that it covers the setup of Postgres and MySql-based applications, but not Oracle. In order to get that done I left my database.yml as before, but added a line to config/environment.rb:

ENV['NLS_LANG']='american_america.AL32UTF8'

This sets an environment variable (it also works if you set the variable for your Rails server process externally). I had to figure out the value of this variable by querying the database:

select * from nls_database_parameters;

For the line in environment.rb use the parameters as: (NLS_LANGUAGE)_(NLS_TERRITORY).(NLS_CHARACTERSET)

OK, I'm happy with my Rails again.

2 comments:

herr ernst said...

thanks so much, you saved my life!

Hire Ruby on Rails Programmer said...

Worth reading and i like this "COFFEE & GEMS" :)