Prev | Current Page 373 | Next

Brad Ediger

"Advanced Rails"

Since Rails 1.2, ActiveRecord correctly
processes UTF-8 data and is ready for UTF-8 storage over supported connections. The
specifics differ among database engines, so we??™ll examine MySQL and PostgreSQL here.
MySQL
To properly store UTF-8 data in a MySQL database, two things need to be in place.
First, the database and tables need to be configured with the proper encoding. Secondly,
the client connection between ActiveRecord and MySQL needs to use UTF-8.
MySQL ships with Latin1 (ISO-8859-1) as the default character set. Thus, all of the
string operations are by default byte-oriented. You can change the default character
set and collation for the entire database server with the following commands in the
MySQL configuration file (my.cnf):
character-set-server=utf8
default-collation=utf8_unicode_ci
* http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
??  http://www.igvita.com/blog/2007/04/11/secure-utf-8-input-in-rails/
246 | Chapter 8: i18n and L10n
The Rails create_database schema definition method will attempt to do the
right thing. If you use create_database to create your databases, they will default to
UTF-8:
>> ActiveRecord::Schema.define do
?> create_database :test_db
>> end
-- create_database(:test_db)
SQL (0.000585) CREATE DATABASE `test_db` DEFAULT CHARACTER SET `utf8`
-> 0.0008s
=> nil
However, the create_table method does not specify a character set, but you can provide
an :options parameter that specifies any table creation options, including a
character set.


Pages:
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385