Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to serialize as ActiveSupport::HashWithIndifferentAccess anymore

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.

like image 251
ctide Avatar asked Jan 18 '12 22:01

ctide


2 Answers

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.

like image 69
mtalcott Avatar answered Sep 19 '22 19:09

mtalcott


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.

like image 24
ctide Avatar answered Sep 18 '22 19:09

ctide