# Fixtures, how to set a serialized Hash object

**URL:** <https://www.ruby-forum.com/t/fixtures-how-to-set-a-serialized-hash-object/144209>\
**Category:** Rails\
**Created:** [July 26, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/fixtures-how-to-set-a-serialized-hash-object/144209 "2008-07-26T18:24:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 26, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/fixtures-how-to-set-a-serialized-hash-object/144209/1 "2008-07-26T18:24:20Z")

</div>

I defined in my model :  
…  
serialize :unavailables  
…

which is set like this (Hash : “year” =\> 2-dimensional Array)  
{“2008” =\> Array.new(13).map!{ Array.new(32, 1)} }

In my fixture I need to initiliaze this field with the following  
hash :

{“2008” =\> Array.new(4).map!{ Array.new(32, 1)} + Array.new(1).map!  
{ [1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1,  
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] } + Array.new(8).map!  
{ Array.new(32, 1)} }

I tried to write :

\<%= Hash.new({“2008” =\> Array.new(10).map!{ Array.new(32, 1)} +  
Array.new(1).map!{ [1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
-1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] } +  
Array.new(2).map!{ Array.new(32, 1)} }) %\>  
but it gives me a full string :  
myobject.unavailables  
“200811111111111111111111111111111111111111111111111…”

I also tried :  
\<%= Hash.new({“2008” =\> Array.new(10).map!{ Array.new(32, 1)} +  
Array.new(1).map!{ [1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
-1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] } +  
Array.new(2).map!{ Array.new(32, 1)} }).to\_yaml %\>  
but it gives me an empty hash :  
myobject.unavailables  
{}

any clue ?

thanks

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 26, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/fixtures-how-to-set-a-serialized-hash-object/144209/2 "2008-07-26T23:43:23Z")

</div>

I found a workaround…

I defined a ruby function using irb  
def unvailable\_period(year, month, day\_start, day\_end)  
v = {"#{year}" =\> (Array.new(month).map!{ Array.new(32, 1)} +  
Array.new(1).map!{ ( [1] + Array.new(day\_end - day\_start + 1, -1) +  
Array.new(32 - day\_end - 1,1) ) } + Array.new(12-month).map!  
{ Array.new(32, 1)}) }  
end

and i used it to ouput a yaml structure  
v = unvailable\_period(2008, 10, 1,15)  
v.to\_yaml  
=\> “— \n"2008”: \n- - 1\n - 1\n - 1\n - 1\n - 1\n - 1\n -  
1\n - 1\n - 1\n - 1\n - 1\n - 1\n - 1\n - 1\n - 1\n - 1\n -  
1\n - 1\n - 1\n - 1\n - 1\n - 1\n - 1\n … etc  
then I copied/pasted it into my fixture, but it seems to be only a  
workaround…

I tried to define the unvailable\_period() function into a lib/  
unavailable\_test\_helper.rb ,  
and include in my class Test::Unit::TestCase ( test\_helper.rb)  
but I could not use it from the fixture (error : not found), is  
there a way to do it ?

writing in my fixture :  
unavailables: \<%= unvailable\_period(2008, 10, 1,15).to\_yaml %\>  
to get the all structure ?
