Unable to serialize as ActiveSupport::HashWithIndifferentAccess anymore

谁说我不能喝 提交于 2019-12-04 03:40:12

问题


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.)

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           |
+----+------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+

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

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:


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

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.




回答2:


Apparently this is just straight up broken with 1.9.2-p290.

Upgrading to 1.9.3, or downgrading to 1.8.7 and everything's peachy. I'd love a better answer than that, though, if anyone has any ideas.




回答3:


If you can, switch over to syck.

In application.rb:

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



回答4:


Just in case anyone has the same problem I've been having, which is similar to this but not the same, I'm posting the answer here. I have a hash which is serialized in the model, which is saving correctly to the db. In one interface I need to regenerate the hash in JS, which would then create entries in the db containing !map:ActiveSupport::HashWithIndifferentAccess rather than the correct data. I am generating the hash as a string and then converting to a hash using eval. This created a Hash, as required. But I was then putting the hash back into the params hash, so I could use update_attributes in the controller which was converting it to HashwithIndifferentAccess, causing the problem



来源:https://stackoverflow.com/questions/8918434/unable-to-serialize-as-activesupporthashwithindifferentaccess-anymore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!