# Generate model Test stats:hash/array? Is it possible?

**URL:** <https://www.ruby-forum.com/t/generate-model-test-stats-hash-array-is-it-possible/171523>\
**Category:** Rails\
**Created:** [July 4, 2009, 12:53am UTC](https://www.ruby-forum.com/t/generate-model-test-stats-hash-array-is-it-possible/171523 "2009-07-04T00:53:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [July 4, 2009, 12:53am UTC](https://www.ruby-forum.com/t/generate-model-test-stats-hash-array-is-it-possible/171523/1 "2009-07-04T00:53:55Z")

</div>

Is it possible to generate a model that way? Instead of,  
Test stat1:string stat2:integer stat3:string,  
is there a way to store arrays/hashes?

Would I just use ‘integer[]’ or etc? What if I wanted an array of  
different types? _sob_

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [July 4, 2009, 1:37am UTC](https://www.ruby-forum.com/t/generate-model-test-stats-hash-array-is-it-possible/171523/2 "2009-07-04T01:37:18Z")

</div>

Hi Tim,

On Sat, 2009-07-04 at 00:53 +0200, Tim M. wrote:

> Is it possible to generate a model that way? Instead of,  
> Test stat1:string stat2:integer stat3:string,  
> is there a way to store arrays/hashes?

I’m really not sure what you’re asking, but I’ll try and I’ll explain a  
little that might help you ask again, better, if you need to.

I assume your question is about script/generate model …

That’s generating a bunch of things for you, including a migration that,  
when run, is going to create a table in a relational database with  
fields named and typed the way you specified. The model created is  
initially just a two line text file that Rails uses to know which table  
to ‘talk to.’

So are you asking about storing arrays/hashes in a single field in a  
relational database? If so, see the ‘serialize’ method. You’d type the  
field as a blob.

> Would I just use ‘integer[]’ or etc? What if I wanted an array of  
> different types? _sob_

In Ruby, which Rails is just helping you write, arrays can contain  
elements of different types.

irb(main):001:0\> arraytest = Array.new  
=\> []  
irb(main):002:0\> arraytest[0] = 1  
=\> 1  
irb(main):003:0\> arraytest[1] = ‘bob’  
=\> “bob”  
irb(main):004:0\> hash\_element = Hash.new  
=\> {}  
irb(main):005:0\> hash\_element = {:color =\> ‘green’}  
=\> {:color=\>“green”}  
irb(main):006:0\> arraytest[2] = hash\_element  
=\> {:color=\>“green”}  
irb(main):007:0\> arraytest.inspect  
=\> “[1, “bob”, {:color=\>“green”}]”

HTH,  
Bill
