Antimony
Antimony

Reputation: 39461

Rails model containing list of integers

How can I have a list of integers as an attribute of a Rails model? I've tried Googling, but nothing comes up. Do I really have to create a seperate model just to hold the integer and then use a has_many relation?

What if I want to have multiple lists in the model? Do I create a seperate model for each list?

What I'm trying to do is create models to represent the state of a board game called Ra. The variables I came up with are

Game
    epoch - int
    currentturn - int
    auctionturn - int
    ratrack - int
    centralsun  - int
    auctiontrack - list[int]
    playerdata - list[playerdata]

PlayerData
    name - string
    suns - list[int]
    nextsuns - list[int]
    gods - int
    pharohs - int
    niles - int
    floods - int
    gold - int
    civilization - list[int]
    monuments - list[int]
    fame - int

But I'm not sure how to implement this. Also, does anyone have suggestions to improve the design?

Upvotes: 3

Views: 1132

Answers (1)

MrTheWalrus
MrTheWalrus

Reputation: 9700

You can store any object in a a database field using serialize: http://apidock.com/rails/ActiveRecord/AttributeMethods/Serialization/ClassMethods/serialize

Support for actual array objects (in PostGres, at least) is going to be added in Rails 4, by the way: http://reefpoints.dockyard.com/ruby/2012/09/18/rails-4-sneak-peek-postgresql-array-support.html

Upvotes: 2

Related Questions