[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Serializing a nested hash

Keith Tom

3/23/2007 5:39:00 PM

Hi all,

I ran into some trouble w/ a model that has a nested hash attribute and
need some help.
Here are the details:

- migration has "t.column :attribute, :text"
- model has "serialize :attribute, Hash"

Now when I put a plain (non-nested) hash in my fixtures, this attribute
works fine; I run the tests, it unserializes, and am very happy.
When I put something like this in the fixture:

attribute: "<%= { :date => 1, :items => 2}.to_yaml %>"

I run the tests and get:

Exception: attribute was supposed to be a Hash, but was a String
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:1951:in
`unserialize_attribute'

I checked and the string that is being returned is:

--- :items: 2 :date: 1

I did some googling and was under the impression nested hashes are
okay... I get the feeling that is wrong...

Thanks in advance guys!
Keith

3 Answers

Rick DeNatale

3/23/2007 8:23:00 PM

0

On 3/23/07, Keith Tom <keith.tom@gmail.com> wrote:
> Hi all,
>
> I ran into some trouble w/ a model that has a nested hash attribute and
> need some help.
> Here are the details:
>
> - migration has "t.column :attribute, :text"
> - model has "serialize :attribute, Hash"
>
> Now when I put a plain (non-nested) hash in my fixtures, this attribute
> works fine; I run the tests, it unserializes, and am very happy.
> When I put something like this in the fixture:
>
> attribute: "<%= { :date => 1, :items => 2}.to_yaml %>"
>
> I run the tests and get:
>
> Exception: attribute was supposed to be a Hash, but was a String
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/active_record/base.rb:1951:in
> `unserialize_attribute'
>
> I checked and the string that is being returned is:
>
> --- :items: 2 :date: 1
>
> I did some googling and was under the impression nested hashes are
> okay... I get the feeling that is wrong...

You can't nest yaml that way, nesting is indicated by indention, and
the --- indicates the start of a yaml document:

irb(main):005:0> {:attribute => {:date => 1, :items => 2}}.to_yaml
=> "--- \n:attribute: \n :items: 2\n :date: 1\n"

irb(main):006:0> puts ({:attribute => {:date => 1, :items => 2}}.to_yaml)
---
:attribute:
:items: 2
:date: 1
=> nil


Not sure what the solution is, but I think that's the problem.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Christoffer Lernö

3/24/2007 11:04:00 AM

0

On Mar 23, 2007, at 21:23 , Rick DeNatale wrote:

> On 3/23/07, Keith Tom <keith.tom@gmail.com> wrote:
>> Hi all,
>>
>> I ran into some trouble w/ a model that has a nested hash
>> attribute and
>> need some help.
>> Here are the details:
>>
>> - migration has "t.column :attribute, :text"
>> - model has "serialize :attribute, Hash"
>>
>> Now when I put a plain (non-nested) hash in my fixtures, this
>> attribute
>> works fine; I run the tests, it unserializes, and am very happy.
>> When I put something like this in the fixture:
>>
>> attribute: "<%= { :date => 1, :items => 2}.to_yaml %>"
>>
>> I run the tests and get:
>>
>> Exception: attribute was supposed to be a Hash, but was a String
>> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/lib/
>> active_record/base.rb:1951:in
>> `unserialize_attribute'
>>
>> I checked and the string that is being returned is:
>>
>> --- :items: 2 :date: 1
>>
>> I did some googling and was under the impression nested hashes are
>> okay... I get the feeling that is wrong...
>
> You can't nest yaml that way, nesting is indicated by indention, and
> the --- indicates the start of a yaml document:
>
> irb(main):005:0> {:attribute => {:date => 1, :items => 2}}.to_yaml
> => "--- \n:attribute: \n :items: 2\n :date: 1\n"
>
> irb(main):006:0> puts ({:attribute => {:date => 1, :items =>
> 2}}.to_yaml)
> ---
> :attribute:
> :items: 2
> :date: 1
> => nil
>
>
> Not sure what the solution is, but I think that's the problem.

If one wants to put a hash (or an array) on a single line with yaml,
you need to use the {} / [] format.

So this:
---
:attribute:
:items: 2
:date: 1

Can be written
:attribute: { :items: 2, :date: 1 }

I.e.

YAML::load(":attribute: { :items: 2, :date: 1 }")
=> {:attribute=>{:items=>2, :date=>1}}

YAML::load("---\n:attribute:\n :items: 2\n :date: 1")
=> {:attribute=>{:items=>2, :date=>1}}


/Christoffer



Rick DeNatale

3/26/2007 7:19:00 PM

0

On 3/24/07, Christoffer Lernö <lerno@dragonascendant.com> wrote:

>
> If one wants to put a hash (or an array) on a single line with yaml,
> you need to use the {} / [] format.
>
> So this:
> ---
> :attribute:
> :items: 2
> :date: 1
>
> Can be written
> :attribute: { :items: 2, :date: 1 }

So the OP could change this to:

attribute: {items: 2, date: 1}

On the other hand it might be nice to be able to use rails feature of
running things like these fixture yaml file's through erb/eruby first,
but that would require a facility (in the yaml module?) to produce an
inline yaml string.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...