由于到目前为止我完全无法解读的原因,我不再能够使用ActiveSupport::HashWithIndifferentAccess。
模型的相关部分如下所示:
class Item < ActiveRecord::Base
serialize :metadata, ActiveSupport::HashWithIndifferentAccess
(我添加了尝试将其强制执行的选项,但并没有帮助。以前,这一切都很好,并且在那儿我还没有。)
只要对象在内存中,一切就可以正常工作。正确地,它是一个HashWithIndifferentAccess,而且生活很好。将其保存到数据库后,将其另存为哈希:
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。我可以添加任何人都认为有帮助的信息,但是对于如何进一步调试它,我一无所知。
最佳答案
1.9.2通常包括Psych作为YAML库。但是,libyaml是一个外部依赖关系,如果在编译Ruby时libyaml不可用,则1.9.2默认使用Syck(旧库):link
Psych YAML将ActiveSupport::HashWithIndifferentAccess用作标准哈希的事实是Psych的Github上的oustanding issue。似乎已在1.9.3中修复。