problem store hash field

Hi, I have an application in rails3 and i have a problem with store a hash. I create a form with these two field

<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="" >

The problem is when i call create and make save i have this string in database for the field birthplace that is wrong

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n!binary \"aXQ=\"

Any idea?

First look in development.log and check that the parameters are correct in the request. If they look ok then debug the create method and see where it is failing. You can easily do simple debugging by inserting into the code things like logger.info some_variable.inspect Then that will appear in the log

Colin

Hi, if I try params.inspect i see

{"utf8"=>"✓",

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

So I think is correct but in database i have

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

I try to change

<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="" >

with

<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="" >

but the result is the same

I have just realised what you are trying to do. Are you trying to save a hash object in a field in the database? I have never done that as almost always you are better to use models and associations for storing the data. That is the Rails way. If you really need to do that then google for how to save a hash in the database using rails, for how to use serialisers to do it.

Colin

Postgresql 9.2 has JSON objects, 9.4 introduces JSONB, but they are often more hassle than they are worth. See the Postgresql docs.

ActiveRecord can also serialize/deserialze hashs (or any other sort of Ruby non-primitive), but it’s also often more hassle than it’s worth. See http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize for the latter.

I’ll very much second Colin’s first point: more analysis and better understanding of the data structures generally give better results. (i.e., more testable, extendable, maintainable, to my mind)

Hi Thank for the response!!!

I make

  serialize :birthplace, Hash

This store the date like {it:"value",de:"value"} and this work.

I found the problem :

The default serialization that is YAML.

I don't know why this not working I try also only for see if Yaml have a problem the instruction YAML.dump(params[:person][:birthplace]) and i have the error !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nit: like when save the data.

My problem is that there are another application that use YAML and i want to merge the two database and is a problem because the format of the fields are differents {} again string.

I don't know why this work in an application and not in this.