Rails糟糕的哈希序列化......有时候?

时间:2013-07-05 12:43:30

标签: ruby-on-rails ruby-on-rails-3 serialization hash ruby-on-rails-3.1

我无法理解为什么序列化有时产生这个(我想要):

--- !map:ActiveSupport::HashWithIndifferentAccess 
first_question: "Pas \xC3\xA9quilibr\xC3\xA9 -perte"
second_question: "Compte de r\xC3\xA9sultat"

有时候(我不想要):

---
first_question: ! "1- Mettre en place le renvoi de son téléphone vers sa boite vocale\r\n2-
  Se mettre dans un bureau fermé pour travailler\r\n3- Savoir dire non"
second_question: ! "1- mes collègues\r\n2- le téléphone\r\n"

我最近进行了全新部署,现在数据存储没有 !map:ActiveSupport :: HashWithIndifferentAccess部分,即 打破我的应用程序!

的Gemfile

ruby '1.9.2' # added this line 3 days ago...
gem 'rails', '3.1.10'

课程模式

# encoding: utf-8
class Course < ActiveRecord::Base
  # [...]
  serialize :estart_scenario_data, Hash
  # [...]
  validate :validate_estart_scenario_data
  # [...]
  def add_data(prefix, params, is_done=true)
    self["#{prefix}_data"] = params
    self["#{prefix}_done_at"] = Time.now if is_done
  end
  # [...]
  private
    def validate_estart_scenario_data
      unless self.estart_scenario_data.blank?
        errors.add(:first_question, "Answer the first question") if self.estart_scenario_data[:first_question].blank?
        errors.add(:second_question, "Answer the second question") if self.estart_scenario_data[:second_question].blank?
      end
    end
end

我曾经用这个得到价值:

self.estart_scenario_data[:first_question]

现在它什么也没产生。我必须改变这一行:

self.estart_scenario_data["first_question"]

但是,它产生错误“UTF-8中的无效字节序列”。

发生什么事了? 现在我在DB中有两种Hash!

我该如何还原呢?

2 个答案:

答案 0 :(得分:0)

psyckruby 1.9.2效果不佳。 我不得不升级到ruby 1.9.3

答案 1 :(得分:0)

--- !map:ActiveSupport::HashWithIndifferentAccess表示这是ActiveSupport::HashWithIndifferentAccess的实例,第二个表示它是Hash的实例。

您可能正在存储来自您的控制器的params,这是ActiveSupport::HashWithIndifferentAccess的实例

相关问题