Serialized attributed with native hash is saved as !ruby/hash:ActiveSupport::HashWithIndifferentAcce

54 views
Skip to first unread message

Alessio Varalta

unread,
May 23, 2016, 8:12:33 AM5/23/16
to Ruby on Rails: Talk
Hi, I have a Rails 3 application. I want to save multiple values in a filed. But when i save I have this error in the field 


--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nit: Padova\nde:
Padovan\n

this is my html

<input type="text" size="25" name="person[birthplace[it]]" id="person_birthplace[it]" value="" > <input type="text" size="25" name="person[birthplace[de]]" id="person_birthplace[de]" value="" >

My params 

{"utf8"=>"✓",
"authenticity_token"=>"RvpwQw/RdTAT2d4jtaSyBAYRgT1mKgWg9kyrS7pUnMo=",
"person"=>{"birthplace"=>{"it"=>"Padova", "de"=>"Padovan"}},
"commit"=>"Create Person", "action"=>"create", "controller"=>"people"}

I have another application where the data is save in this format for example

---\nit: Padova\nde: Padovan\n


and i don't have any error.


If i put in the model


serialize :birthplace, Hash


is correct but i have a diffrent format {:it=>"Padova",:de=>"Padovan"}


I read in the last part of this document

http://apidock.com/rails/ActiveRecord/AttributeMethods/Serialization/ClassMethods/serialize 

that Yaml is the default serialization and generate a string ---\n....


Any ideas?








Frederick Cheung

unread,
May 24, 2016, 5:35:54 AM5/24/16
to Ruby on Rails: Talk


On Monday, May 23, 2016 at 1:12:33 PM UTC+1, Alessio Varalta wrote:

If i put in the model


serialize :birthplace, Hash


is correct but i have a diffrent format {:it=>"Padova",:de=>"Padovan"}


I read in the last part of this document

http://apidock.com/rails/ActiveRecord/AttributeMethods/Serialization/ClassMethods/serialize 

that Yaml is the default serialization and generate a string ---\n....



This is the right thing to do. You don't see the yaml in your app because rails is doing the conversion to/from yaml for you. If you were to look at your column with a non rails database tool then you would see the raw serialised data.

Fred
Reply all
Reply to author
Forward
0 new messages