在MySQL的字符集" utf8mb4",甚至rails控制台运行良好,但seed_fu运行不好

时间:2015-12-19 12:23:36

标签: ruby-on-rails console utf8mb4

关于MySQL的字符集" utf8mb4",
rails console运行良好。

<pre>
<p style="background-color:#FFFFFF;border:1px solid #999999;padding:5px;">
10191% rails c -e production                                                                                                                                   Loading production environment (Rails 4.2.5)
irb(main):001:0> skin = Skin.first
  Skin Load (203.9ms)  SELECT  `skins`.* FROM `skins`  ORDER BY `skins`.`id` ASC LIMIT 1
=> #<Skin id: 1, created_at: "2015-12-19 07:59:48", updated_at: "2015-12-19 08:08:34", name: "", file: "standard.css", expression: "">
irb(main):002:0> skin.name = 'たのしい''
irb(main):003:0'
irb(main):004:0' '
=> "たのしい\n\n"
irb(main):005:0> skin.name = 'たのしい'
=> "たのしい"
irb(main):006:0> skin.save
   (112.3ms)  BEGIN
  SQL (115.0ms)  UPDATE `skins` SET `name` = 'たのしい', `updated_at` = '2015-12-19 11:56:18' WHERE `skins`.`id` = 1
   (105.7ms)  COMMIT
=> true
</p>
</pre>


但是&#34; rake db:seed_fu&#34;没有像这样运行得很好。

<pre>
<p style="background-color:#FFFFFF;border:1px solid #999999;padding:5px;">
10192% rake db:seed_fu RAILS_ENV=production
== Seed from /Users/js/work/picrating/db/fixtures/production/publish.rb
 - Publish {:id=>1, :name=>"公開"}
 - Publish {:id=>2, :name=>"非公開"}

== Seed from /Users/js/work/picrating/db/fixtures/production/skin.rb
 - Skin {:id=>1, :name=>"standard", :file=>"standard.css", :expression=>"レギュラー"}
 - Skin {:id=>2, :name=>"light", :file=>"light.css", :expression=>"たのしい"}
rake aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect string value: '\xF0\x9F\x8C\xBB' for column 'expression' at row 1: INSERT INTO `skins` (`id`, `name`, `file`, `expression`, `created_at`, `updated_at`) VALUES (2, 'light', 'light.css', 'たのしい', '2015-12-19 12:08:05', '2015-12-19 12:08:05')
(eval):1:in `block (2 levels) in run_file'
Mysql2::Error: Incorrect string value: '\xF0\x9F\x8C\xBB' for column 'expression' at row 1
(eval):1:in `block (2 levels) in run_file'
Tasks: TOP => db:seed_fu
(See full trace by running task with --trace)

</p>
</pre>


我不知道,甚至rails控制台运行良好,
但db:seed_fu运行不好 发生了什么事?

1 个答案:

答案 0 :(得分:0)

这是一个充分的线索吗?

application_controller.rb

  def configure_charsets
    response.headers["Content-Type"] = "text/html; charset=utf-8"
    suppress(ActiveRecord::StatementInvalid) do
      ActiveRecord::Base.connection.execute 'SET NAMES utf8mb4'
    end
  end