0

I'm trying to save a nested Hash to my database and retrieve it, but nested values are lost upon retrieval.

My model looks like this:

class User
  serialize :metadata, MetaData
end

The class MetaData looks like this:

class MetaData < Hash
  attr_accessor :availability, :validated
end

The code I'm using to store data looks something like this (the real data is coming from a HTML form, though):

user = User.find(id)
user.metadata.validated = true
user.metadata.availability = {'Sunday' => 'Yes', 'Monday' => 'No', 'Tuesday' => 'Yes'}
user.save

When I look at the data in the database, I see the following:

--- !map:MetaData 
availability: !map:ActiveSupport::HashWithIndifferentAccess 
  Sunday: "Yes"
  Monday: "No"
  Tuesday: "Yes"
validated: true

The problem occurs when I try to get the object again:

user = User.find(id)
user.metadata.validated # <- this is true
user.metadata.availability # <- this is nil

Any ideas? I'm using Rails 3.1 with Postgresql as my datastore.

2 Answers 2

1

If you look in the database you see "map:ActiveSupport::HashWithIndifferentAccess" for availability?

My approach would be to separate out the single instance of availablity from the hash collection structure of days available.

Sign up to request clarification or add additional context in comments.

2 Comments

It's coming from an HTML form as a HashWithIndifferentAccess. Are you suggesting to convert it from this to a regular Hash? and store that instead?
I tried converting it to a regular Hash and am still having the same problem.
0

you mean user.metadata.validated # <- this is true ?

What DB columns are metadata and availability stored as? They need to be TEXT

1 Comment

There is only 1 column, called metadata. It is of type text. Availability is a key on the hash that I'm storing in metadata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.