Ruby1.9 + Rails2.5 マルチバイト文字列でエラー
MySql/Rubyがruby1.9のM17Nに対応していないため
DBから取り出した文字列がすべて「ASCII-8BIT」になってしまいます。
そのため、マルチバイト文字と混在させると、下記のようなエラーが発生します。
「incompatible character encodings: UTF-8 and ASCII-8BIT」
これを暫定的に回避するために、force_encodingして対応します。
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb
def define_read_method(symbol, attr_name, column) ... # access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']" access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "(@attributes['#{attr_name}'].class == String) ? @attributes['#{attr_name}'].force_encoding('UTF-8') : @attributes['#{attr_name}']" ... end def read_attribute(attr_name) ... # column.type_cast(value) (column.type_cast(value).class == String) ? column.type_cast(value).force_encoding("UTF-8") : column.type_cast(value) ... end
さらにビューのほうにエンコード情報を与えます。
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_view/renderable.rb
def compile!(render_symbol, local_assigns) ... source = <<-end_src # encoding: utf-8 def #{render_symbol}(local_assigns) old_output_buffer = output_buffer;#{locals_code};#{compiled_source} ensure self.output_buffer = old_output_buffer end end_src ... end