problem store hash field

34 views
Skip to first unread message

Alessio Varalta

unread,
May 12, 2016, 4:04:09 AM5/12/16
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
May 12, 2016, 5:53:20 AM5/12/16
to Ruby on Rails: Talk
On 12 May 2016 at 09:03, Alessio Varalta <li...@ruby-forum.com> wrote:
> 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

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

Alessio Varalta

unread,
May 18, 2016, 3:51:35 AM5/18/16
to rubyonra...@googlegroups.com
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

Colin Law

unread,
May 18, 2016, 4:17:26 AM5/18/16
to Ruby on Rails: Talk
On 18 May 2016 at 08:51, Alessio Varalta <li...@ruby-forum.com> wrote:
> 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 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

tamouse pontiki

unread,
May 18, 2016, 9:18:55 AM5/18/16
to rubyonra...@googlegroups.com

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)

Alessio Varalta

unread,
May 19, 2016, 11:07:56 AM5/19/16
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages