HashWithIndifferentAccess

HashWithIndifferentAccess

本文介绍了无法再序列化为 ActiveSupport::HashWithIndifferentAccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于到目前为止我完全无法破译的原因,我不能再使用 ActiveSupport::HashWithIndifferentAccess.

For reasons that I'm so far completely unable to decipher, I'm no longer able to use ActiveSupport::HashWithIndifferentAccess anymore.

模型的相关部分如下所示:

The relevant part of model looks like this:

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

(我添加了尝试强制执行的选项,但没有帮助.以前这一切正常,而我没有.)

(I added the option to try and force it along, but it hasn't helped. Previously this was all working fine, and I didn't have that there.)

只要对象在内存中,一切正常.它正确地是一个 HashWithIndifferentAccess,并且生活是美好的.一旦它被保存到数据库中,它就会被保存为一个哈希:

For as long as the object is in memory, everything works fine. It's correctly a HashWithIndifferentAccess, and life is good. Once it gets saved to the database, it's saved as a Hash instead:

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 并且事情会像这样爆炸:

Which means it can't be coerced back into a HashWithIndifferentAccess and things blow up like this:

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.我可以添加更多任何人认为有帮助的信息,但我不知道如何进一步调试.

This is using Rails 3.1.3, storing the data in MySQL using the mysql2 gem version 0.3.10. I'm running ruby 1.9.2p290 as well. I can add anymore information that anyone would deem helpful, but I'm at a loss as to how to debug this further.

推荐答案

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

1.9.2 normally includes Psych as the YAML library. However, libyaml is an external dependency, and 1.9.2 defaults to using Syck (the old library) if libyaml is not available when Ruby is compiled: link

Psych YAML 将 ActiveSupport::HashWithIndifferentAccess 作为标准哈希的事实是一个悬而未决的问题 在 Psych 的 Github 上.不过似乎在 1.9.3 中已修复.

The fact that Psych YAML-izes ActiveSupport::HashWithIndifferentAccess as a standard hash is an oustanding issue on Psych's Github. Seems to be fixed in 1.9.3 though.

这篇关于无法再序列化为 ActiveSupport::HashWithIndifferentAccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 13:37