Can't store UTF-8 in RDS despite setting up new Parameter Group using Rails on Heroku

前端 未结 2 1407
广开言路
广开言路 2021-02-10 14:46

I\'m setting up a new instance of a Rails(2.3.5) app on Heroku using Amazon RDS as the database. I\'d like to use UTF-8 for everything. Since RDS isn\'t UTF-8 by default, I set

相关标签:
2条回答
  • 2021-02-10 14:54

    Ultimately I solved my problem by adding the following in the Rails::Initializer.run block in the 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 doesn't include any encoding or coalition settings. By hacking it thusly, the correct settings are always merged in before the database connection is made.

    0 讨论(0)
  • 2021-02-10 15:14

    There's a simpler way. You can specify the encoding in your DB connection string. Edit the RDS add-on, and append ?encoding=utf8&collation=utf8_general_ci

    Worked well for me, no changes to the project.

    e.g.:

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

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

    0 讨论(0)
提交回复
热议问题