In order to coerce Rails 1.2.x into supporting Unicode values for an
attribute in a model, I found I had to do the following:
-
Change the underlying column type from varchar to nvarchar.
-
Prepend any quoted values going into nvarchar columns with a
capital-letter-N, e. g. ‘unicode’ becomes N’unicode’ In order to
accomplish this, I modified SQLServerAdapter#quote to look like this:def quote(value, column = nil)
return value.quoted_id if value.respond_to?(:quoted_id)case value when TrueClass then '1' when FalseClass then '0' when Time, DateTime then "'#{value.strftime("%Y%m%d
%H:%M:%S")}’"
when Date then “’#{value.strftime(”%Y%m
%d")}’"
when String, ActiveSupport::Multibyte::Chars
qvalue = super
qvalue = “N#{qvalue}” if !column.nil? && column.sql_type
=~ /\Anvarchar/
qvalue
else
super
end
end
This passed the several tests I wrote for it and otherwise seemed to
pass the couple of AR tests I ran against it. (There’s another nice
bug in the SQL Server adapter that puts the value ‘NULL’ into string
columns instead of the actual value NULL which causes the polymorphic
model tests to fail.)
My questions are:
-
Does this look right?
-
Can this be improved?
-
Can this be moved into a Rails plugin so it isn’t a monkey patch to
AR? -
Has someone else done this already?
Thanks.
Michael