0

我不明白爲什麼有時候序列化生產這個(我想):Rails壞哈希序列化...有時候?

--- !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中的無效字節序列」。

發生了什麼事? 現在我有2種哈希在數據庫中!

我該如何恢復?

+0

紅寶石1.9.2p290壞序列化的一個實例存儲params,似乎有病有心理這裏有一個問題。我會在我的應用程序中嘗試'YAML :: ENGINE.yamler ='syck'' ... –

回答

0

psyckruby 1.9.2打得不好。 我不得不升級到ruby 1.9.3

0

--- !map:ActiveSupport::HashWithIndifferentAccess意味着這是一個ActiveSupport::HashWithIndifferentAccess實例,第二個意味着它的Hash一個實例。

你可能會從你的控制器,這是ActiveSupport::HashWithIndifferentAccess

+0

當然,我明白這一點。但我無法弄清楚爲什麼......因爲除了Gemfile之外我沒有改變任何東西。 –

+0

發現這個:Psych YAML -Aizes ActiveSupport :: HashWithIndifferentAccess作爲一個標準的哈希是Psycho Github上的一個問題。似乎固定在1.9.3。 http://stackoverflow.com/questions/8918434/unable-to-serialize-as-activesupporthashwithindifferentaccess-anymore –