Replacing –, ’, “, etc., with UTF-8 Characters in Ruby on Rails

November 07, 2011 – tagged as characters, encoding, rails, ruby, utf8

Recently I upgraded some older Rails applications to Rails 3.1 and Ruby 1.9.2 (from 2.3 and 1.8.7 respectively). One post-upgrade issue was that text content had a lot of garbage showing up like –, ’, “, etc. For example, here’s an actual example from a comment in one of the applications:

One of my “things to do before I’m 50” is

This should read:

One of my “things to do before I’m 50” is

It turns out these are just special characters that were improperly encoded for utf-8. The fix is simple enough: loop through your content and replace where needed.

If your database is big, this could take a long time unless you disable callbacks. The script below highlights both how to replace the characters using Ruby and how to disable your Rails callbacks to make this script run in seconds instead of hours (depending on the complexity of your callbacks).

replacements = []
replacements << ['…', '…']           # elipsis
replacements << ['–', '–']           # long hyphen
replacements << ['’', '’']           # curly apostrophe
replacements << ['“', '“']           # curly open quote
replacements << [/â€[[:cntrl:]]/, '”'] # curly close quote
klasses = [Comment, Article]           # replace with relevant classes
klasses.each do |klass|
  klass.all.each do |obj|
    original = obj.body
    replacements.each{ |set| obj.body = obj.body.gsub(set[0], set[1]) }
    unless (original == obj.body)
      #### Remove or Customize ####
      # This should reflect your models' callbacks.  It should be safe
      # since we're just doing a simple find/replace.
      Comment.skip_callback(:save, :after,  :do_after_save_tasks ) if obj.is_a?(Comment)
      Article.skip_callback(:save, :before, :do_before_save_tasks) if obj.is_a?(Article)
      #### End Remove or Customize ####! 

If you noticed, I used a regular expression for the curly close quote. This is because there is an invisible control character that is not easily copy/pasted into your code. Using [[:cntrl:]] is just an easier way to catch it.


This article is 7 years old and closed for new comments.

steven shustak • April 16, 2012

I have been getting unusual characters within some email messages which appear randomly, across the board, so to speak. As I recall, they show up in place of the letter “ s “ in most instances. I’ve noticed them for 6 or more months now in “text” and/or in “rich text” documents. According to your information they have been found in: “ “ “ or variations of same characters. In my humble, unsophisticated opinion, they are freak code of some form or other. Kindly advise me if new info comes out. I am most curious. Thanks, Steve Shustak, e/m address:

Piero • April 9, 2013

Great post, but I wouldn’t limit it to Ruby. I just used your hints to fix problems in my SQL dump file, which accidently converted UTF8 characters to those broken ones. Thanks for the post, anyways ;)