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

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=”: Salisburgo\n!binary “ZGU=”: Salisburgo\n”

Any idea?

If i make params.inspect before save i see

“birthplace”=>{“it”=>“Salisburgo”, “de”=>“Salisburgo”}

On 12 May 2016 at 09:03, Alessio V. [email protected] wrote:

database for the field birthplace that is wrong
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

with

but the result is the same

If I try with rails -c

person.birthplace={“it”=>“Salisburgo”, “de”=>“Salisburgo”}
person.save!

This work so the error is when i make

User.new(params[:person])

On May 18, 2016 at 03:17:10, Colin L. ([email protected]) wrote:

On 18 May 2016 at 08:51, Alessio V. [email protected] wrote:

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

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 ActiveRecord::AttributeMethods::Serialization::ClassMethods 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)

On 18 May 2016 at 08:51, Alessio V. [email protected] wrote:

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

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

Hi Thanks for the responses!!!

I try

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 like
default 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.