you can add a before_save method that converts your text to the corresponding UTF-8 characters. if you have only one field that can contain characters other than UTF8, then it is simple, if you have many fields, it would be better if you dynamically iterated over the changed text / string fields and fixed the UTF-8 problem. In any case, you need to use String # encode . Here is an example
before_save :fix_utf8_encoding def fix_utf8_encoding columns = self.class.columns.select{|col| [:text,:string].include?(col.type)}.map{|col| col.name.to_sym} columns.each do |col| self[col] = self.self[col].encode('UTF-8', :invalid => :replace, :undef => :replace) if self[col].kind_of?(String)
And for bonus points, you can also check if the field has been changed using the modified rudders? assistants before correcting it.
source share