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* -- Posted via http://www.ruby-forum.com/.
bill walton
2009-Jul-03 23:36 UTC
Re: generate model Test stats:hash/array? Is it possible?
Hi Tim, On Sat, 2009-07-04 at 00:53 +0200, Tim Mcd 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