I have this json object (which i don’t really know what is inside) and I want to store it into a rails model with an openstruct attribute
so for example with jquery I send this json to the good url:
Data = A_JSON_OBJECT_LIKE_CSV;
myJSONData = {"OpenStructObject":{"content":' + Data + '}}
$.ajax({
type: "POST",
url: 'http://localhost:3000/surveys/',
dataType: 'json',
data: myJSONData,
success: alert('wow : success !')
});
So how should be written this Survey model ?
Maybe something like this :
class Survey < ActiveRecord::Base
serialize :content, OpenStruct
def initialize(idunno)
@content = watever;
end
end
I really don’t know except that It must be possible to do it !
Why do you need this to be converted into OpenStruct and save as yaml in db. Instead JSON converted to hash saved as yaml does the same thing without overhead of open struct.
============================UPDATED[START]
you already have solution in your code. look at this
Here you are serializing content. right!! So it automatically converts assigned hash to yaml before saving. So here is some assignment
Now when you access it back again
just simple as it is. You don’t have to worry about yaml conversion and back, Rails will take care of it.
============================UPDATED[END]