Module: ActiveRecordEncoding

Defined in:
lib/active_record_encoding.rb

Overview

ActiveRecordEncoding — Module to make ActiveRecord aware of Unicode encoding issues on Ruby 1.9. This software is not supported on Ruby 1.8 at all, and probably never will be. It should be used only if the underlying database and its driver does not or cannot properly handle the encoding of the data it returns (usually as “ASCII-8BIT”). Most databases can properly encode data, however, so your first assumption should be that you do not need this software unless you really know you need it.

ActiveRecordEncoding keeps two variables for each column and table where encoding is requested: a required external_encoding value and an optional internal_encoding value.

External encodings must be defined for each column or table where a translation is to occur, and this is done in the model definition:

class User < ActiveRecord::Base
  external_encoding 'ISO-8859-1', :for => :comment
  external_encoding 'ISO-8859-1', :for => [:first_name, :last_name]
end

A similar internal_encoding method exists and specifies what encoding to use internally for each column or table, but this may also be set systemwide in the Rails environment files:

ActiveRecordEncoding.internal_encoding = 'UTF-8'

When data is being saved back to the database, the internal_encoding value is ignored and the encoding of the input is used to determine how to convert data to the external_encoding.

Defined Under Namespace

Modules: ExtendedClassMethods, IncludedInstanceMethods, StandardClassMethods

Class Attribute Summary collapse

Class Attribute Details

.internal_encodingObject

Returns the value of attribute internal_encoding.



41
42
43
# File 'lib/active_record_encoding.rb', line 41

def internal_encoding
  @internal_encoding
end