Setting Activerecord Connection To Utf8 On Heroku
title: Setting Rails ActiveRecord connection encoding to UTF-8 on Heroku
The problem: When a Rails app is deployed to Heroku
a ERB file called database.yml
is added to the app’s config directory.
This ERB file generates the YAML needed for ActiveRecord to make the DB connection based on
a Heroku config variable called DATABASE_URL
. You might recognize this variable
from when you run heroku config
and it is basically a url with settings for the
DB connection. Unfortunately utf-8 encoding is not added to the generated YAML by default.
This is what config/database.yml looks like on Heroku (on the Cedar stack):
A solution: If we read through the code we see that we can add additional attributes to the generated YAML by adding a query string to the url. Sweet!
All we have to do is update the DATABASE_URL
config variable with our encoding settings:</a>
It’s pretty clever of Heroku to use DATBASE_URL like this!