Unable to save UTF-8 to RDS despite setting up a new parameter group using Rails on Heroku

I am creating a new instance of the Rails application (2.3.5) on Heroku using Amazon RDS as the database. I would like to use UTF-8 for everything. Since RDS is not UTF-8 by default, I set up a new parameter group and switched the database to use it, mainly for this . Seems to work:

SHOW VARIABLES LIKE '%character%'; character_set_client utf8 character_set_connection utf8 character_set_database utf8 character_set_filesystem binary character_set_results utf8 character_set_server utf8 character_set_system utf8 character_sets_dir /rdsdbbin/mysql-5.1.50.R3/share/mysql/charsets/ 

In addition, I have successfully configured Heroku to use the RDS database. After rake db: migrate, everything looks good:

 CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `commentable_id` int(11) DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, `content` text COLLATE utf8_unicode_ci, `child_count` int(11) DEFAULT '0', `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `commentable_id` (`commentable_id`), KEY `index_comments_on_community_id` (`community_id`), KEY `parent_id` (`parent_id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

In the markup, I included:

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

In addition, I installed:

 production: encoding: utf8 collation: utf8_general_ci 

... in database.yml, although I'm not very sure that something is being done in this case to honor any of these parameters, since Heroku seems to make its own configuration when connected to RDS.

Now I enter a comment through the form in the application: "Úbe® ƒåiL", but in the database I have "Úbe® ÆÃ ¥ iL"

It looks great when Rails loads it back from the database, and it displays on the page, so that whatever it does in one direction, it cancels the other way. If I look at the RDS database in Sequel Pro, it looks great if I set the encoding to "Unicode UTF-8 via Latin 1". So it looks like Latin-1 is sneaking somewhere out there.

Everything works in development when connected to a local MySQL database.

Someone must have done this before, right? What am I missing?

+4
source share
2 answers

There is an easier way. You can specify the encoding in the DB connection string. Edit the RDS add-in and add ?encoding=utf8&collation=utf8_general_ci

Works well for me, no change to the project.

eg:.

  mysql://user: pass@abc.rds.amazonaws.com /my-db?encoding=utf8&collation=utf8_general_ci 

Link: http://blog.arvidandersson.se/2011/09/27/setting-activerecord-connection-to-utf8-on-heroku

+5
source

I eventually solved my problem by adding the following to the Rails :: Initializer.run block in environment.rb

 class Rails::Configuration def database_configuration # Heroku overwrites the database.yml file without setting any encoding when deploying to outside server (like Amazon RDS) require 'erb' YAML::load(ERB.new(IO.read(database_configuration_file)).result).each_value {|env| env.merge!({"encoding" => "utf8", "collation" => "utf8_general_ci"}) } end end 

Heroku overwrites the database.yml file and does not include any encoding or coalition settings. Thus, breaking it, the correct settings are always combined before connecting to the database.

+2
source

Source: https://habr.com/ru/post/1334423/


All Articles