无法再作为ActiveSupport :: HashWithIndifferentAccess序列化

时间:2012-01-18 22:26:28

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

由于我到目前为止完全无法解密的原因,我再也无法使用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版本0.3.10将数据存储在MySQL中。我也在运行ruby 1.9.2p290。我可以添加任何人认为有用的信息,但我不知道如何进一步调试。

4 个答案:

答案 0 :(得分:4)

1.9.2通常包括Psych作为YAML库。但是,libyaml是一个外部依赖项,如果在编译Ruby时libyaml不可用,则1.9.2默认使用Syck(旧库):link

Psych YAML-izes ActiveSupport :: HashWithIndifferentAccess作为标准哈希的事实是在Psych的Github上oustanding issue。似乎在1.9.3中修复了。

答案 1 :(得分:3)

显然,这只是在1.9.2-p290时被打破了。

升级到1.9.3,或降级到1.8.7,一切都很好。不过,如果有人有任何想法,我会喜欢比这更好的答案。

答案 2 :(得分:0)

如果可以,请切换到syck。

在application.rb中:

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

答案 3 :(得分:0)

以防万一我遇到同样的问题,这与此类似,但不一样,我在这里发布答案。我有一个在模型中序列化的哈希,它正确地保存到数据库。在一个接口中,我需要在JS中重新生成散列,然后在db中创建包含!map:ActiveSupport :: HashWithIndifferentAccess而不是正确数据的条目。我将哈希生成为字符串,然后使用eval转换为哈希。这根据需要创建了一个Hash。但是我当时把哈希放回到params哈希中,所以我可以在控制器中使用update_attributes将它转换为HashwithIndifferentAccess,导致问题