2012-01-18 58 views
5

由於我迄今完全無法破譯的原因,我不再能夠使用ActiveSupport :: HashWithIndifferentAccess了。無法序列化爲ActiveSupport :: HashWithIndifferentAccess

模型的相關部分看起來是這樣的:

class Item < ActiveRecord::Base 
    serialize :metadata, ActiveSupport::HashWithIndifferentAccess 

(我加了嘗試,並迫使它沿選項,但它並沒有幫助以前這是所有工作的罰款,我沒有。」 )

只要對象在內存中,一切工作正常。它正確地是一個HashWithIndifferentAccess,並且生活是美好的。一旦它被保存到數據庫,它保存爲一個Hash來代替:

mysql> select * from items; 
+----+------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+ 
| id | link | text  | metadata                                                                                                                                                                                       | category_id | 
+----+------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+ 
| 1 | NULL | Apple Store | --- 
id: 42cc7080f964a520e9251fe3 
name: Apple Store 
contact: 
    phone: '4153920202' 
    formattedPhone: (415) 392-0202 
location: 
    address: 1 Stockton St. 
    crossStreet: at Ellis St. 
    lat: '37.78573590563453' 
    lng: '-122.40610713227913' 
    distance: '1784' 
    postalCode: '94108' 
    city: San Francisco 
    state: CA 
    country: USA 
categories: 
    '0': 
    id: 4bf58dd8d48988d122951735 
    name: Electronics Store 
    pluralName: Electronics Stores 
    shortName: Electronics 
    icon: https://foursquare.com/img/categories/shops/technology.png 
    parents: 
    - Shops & Services 
    primary: 'true' 
verified: 'false' 
stats: 
    checkinsCount: '30462' 
    usersCount: '16105' 
    tipCount: '128' 
url: http://apple.com/sanfrancisco 
hereNow: 
    count: '7' 
| 1   | 
+----+------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+ 

這意味着它不能被強制回一個HashWithIndifferentAccess和事情鬧成這樣:

ActiveRecord::SerializationTypeMismatch in Index#index 

Showing /development/lists.io/website/app/views/users/_todo.html.haml where line #7 raised: 

Attribute was supposed to be a ActiveSupport::HashWithIndifferentAccess, but was a Hash 

這是使用Rails 3.1.3,使用mysql2 gem version 0.3.10在MySQL中存儲數據。我也運行紅寶石1.9.2p290。我可以添加任何人都會認爲有用的信息,但是我對如何進一步調試這個問題感到茫然。

回答

4

1.9.2通常包括Psych作爲YAML庫。然而,libyaml是外部的依賴,以及1.9.2默認使用SYCK(舊庫)如果libyaml時不可用的Ruby編譯:link

事實上,精極度緊張YAML-izes的ActiveSupport :: HashWithIndifferentAccess作爲標準哈希是Psych's Github上的oustanding issue。似乎固定在1.9.3。

3

顯然這只是與1.9.2-p290斷了直。

升級到1.9.3,或降級到1.8.7,一切都很好。不過,如果有人有任何想法,我會比這更好的回答。

0

如果可以,請切換到syck。

在application.rb中:

require 'yaml' 
YAML::ENGINE.yamler = 'syck' 
0

萬一有人有我一直有同樣的問題,這是類似這一點,但不一樣的,我在這裏張貼的答案。我有一個在模型中序列化的哈希,它正確地保存到數據庫。在一個接口中,我需要在JS中重新生成哈希,然後在包含!map:ActiveSupport :: HashWithIndifferentAccess的數據庫中創建條目,而不是正確的數據。我將哈希生成爲字符串,然後使用eval轉換爲哈希。這根據需要創建了一個Hash。但是當時我把哈希放回到params哈希中,所以我可以在控制器中使用update_attributes,將它轉換爲HashwithIndifferentAccess,導致問題

相關問題