Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

246 | Chapter 8: i18n and L10n


The Rails create_database schema definition method will attempt to do the
right thing. If you usecreate_databaseto 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, thecreate_tablemethod does not specify a character set, but you can pro-
vide an:optionsparameter that specifies any table creation options, including a
character set. (Bear in mind, though, that by specifying DBMS-specific table creation
syntax, you lose portability between DBMSs.)


>> ActiveRecord::Schema.define do
?> create_table :test do end
>> end
-- create_table(:test)
SQL (0.028168) CREATE TABLE `test` (`id` int(11) DEFAULT NULL
auto_increment PRIMARY KEY) ENGINE=InnoDB
-> 0.1264s
=> nil

>> ActiveRecord::Schema.define do
?> create_table :test2, :options =>
'ENGINE=InnoDB DEFAULT CHARSET=utf8' do end
>> end
-- create_table(:test2, {:options=>"ENGINE=InnoDB DEFAULT CHARSET=utf8"})
SQL (0.028386) CREATE TABLE `test2` (`id` int(11) DEFAULT NULL
auto_increment PRIMARY KEY) ENGINE=InnoDB
DEFAULT CHARSET=utf8
-> 0.0287s
=> nil

However, none of these methods will handle preexisting databases. Chances are, if
you have created databases and tables without specifyingCHARACTER SET utf8, the
tables are treating the data as Latin1. If the data is actually Latin1 (and you are now
converting the entire application to Unicode at once), the conversion is simple,
though it must be done once for each table:


ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;

If your only need is straight data conversion, this will work. If you are using
ActiveRecord migrations, Graeme Mathieson has written a migration that will perform
this conversion for every table in your database. It is available fromhttp://woss.name/
2006/10/25/migrating-your-rails-application-to-unicode/.


Be very careful converting a table that has existing data. If you have been using Rails 1.2
or later (which support UTF-8 by default) and have not converted your tables toUTF-8,

Free download pdf