# Modeling a collection of integers in active record

**URL:** <https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676>\
**Category:** Rails\
**Created:** [September 3, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676 "2007-09-03T20:20:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasiu85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jasiu85](https://www.ruby-forum.com/u/jasiu85)\
**Post date:** [September 3, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676/1 "2007-09-03T20:20:42Z")

</div>

Hey,

I’m a newbie to Rails, so forgive me if my question is stupid and I’m  
spamming the group. I couldn’t find any info on the Web.

I’d like to model an entity that contains a collection of integers. In  
SQL, I could achieve that by creating two tables, like that:

CREATE TABLE Entity (  
id INTEGER,  
title VARCHAR,  
other\_stuff BLOB  
);

CREATE TABLE EntityIntegerCollection (  
entity\_id INTEGER,  
value INTEGER  
);

And now I can SELECT value FROM EntityIntegerCollection WHERE  
entity\_id = 1234 to retrieve the collection for entity 1234. My  
question is: How do I model this in ActiveRecord? Can I keep this  
simple table schema or do I have to change something to get what I  
want?

---

<div class="post-metadata">

**Author:** ![jasiu85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jasiu85](https://www.ruby-forum.com/u/jasiu85)\
**Post date:** [September 3, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676/2 "2007-09-03T20:25:29Z")

</div>

Well, you start with creating a model for every table, which can be done  
like when you create controllers.

ruby script/generate model entity  
ruby script/generate model entityintegercollection

and in your migration you can add columns 😃

db/migrate/ (HERE)

---

<div class="post-metadata">

**Author:** ![jasiu85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jasiu85](https://www.ruby-forum.com/u/jasiu85)\
**Post date:** [September 3, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676/3 "2007-09-03T20:56:58Z")

</div>

Uhm, I think I’m not following you…

What I want is to create a SQL table structure, so that later in Rails  
I can write something like entity.integer\_collection[3]. I’m looking  
for a way to express this association using has\_many, belongs\_to or  
something like that… I don’t want to stick to the table schema  
mentioned, but I want to have Entity class that has attribute  
integer\_collection. Do you mean I can achieve this with migrations?

Mike

On Sep 3, 8:25 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jasiu85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jasiu85](https://www.ruby-forum.com/u/jasiu85)\
**Post date:** [September 3, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676/4 "2007-09-03T21:00:00Z")

</div>

Let me first answer your question directly and then suggest an  
alternative.

First of all, let’s follow rails convention here and create two  
tables: entities and entity\_integers. We’ll also create the models  
that rails will use to interface them. We’ll do both at once with:

ruby script/generate model Entity  
ruby script/generate model EntityInteger

You will find two new migrations in db/migrate:

001\_create\_entities.rb  
002\_create\_entity\_integers.rb

In those add your columns:

class CreateEntities \< ActiveRecord::Migration  
def self.up  
create\_table :entities do |t|  
t.column :title, :string  
end  
end

def self.down  
drop\_table :entities  
end  
end

Similarly in your 002\_create\_entity\_integers.rb migration add:

t.column :value, :integer  
t.column :entity\_id, :integer

in your Entity model (app/models/entity.rb) add:

has\_many :entity\_integers

and in your EntityInteger model add:

belongs\_to :entity

Run rake db:migrate and that’s it. open up script/console and play  
around.

@entity = Entity.new :title =\> “foo”  
@entity.save  
@entity.entity\_integers.create :value =\> 1  
@entity.entity\_integers.create :value =\> 2

@entity.entity\_integers.map {|integer| integer.value} #=\> [1, 2]

Now here’s an alternative. Create the same Entity model as before but  
in the migration do:

t.column :title, :string  
t.column :integrers, :string, :default =\> [].to\_yaml # will work in  
MySQL but not Postgres, last time I checked.

and in the Entity model:

seialize :integers, :class\_name =\> Array

and try this out:

@entity = Entity.new :title =\> “foo”  
@entity.integers += [1,2,3]  
@entity.save  
@entity.integers #=\> [1,2,3]  
@entity.integers += [5,6,7]  
@entity.save  
@entity.integers #=\> [1,2,3,5,6,7]

Now @entity.integers is transparently serialized into the database as  
an array.

Just another option.

Rein

---

<div class="post-metadata">

**Author:** ![jasiu85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jasiu85](https://www.ruby-forum.com/u/jasiu85)\
**Post date:** [September 5, 2007, 9:00am UTC](https://www.ruby-forum.com/t/modeling-a-collection-of-integers-in-active-record/113676/5 "2007-09-05T09:00:29Z")

</div>

Thats fantastic Rein,

I wanted something similar to Mike above, at the moment i manually  
unload and load (with YAML) a whole bunch of floats from a binary  
field.  
Your second solution can make my code much cleaner.

I presume the string size can be enlargened with something  
like :limit =\> 1.megabyte in the migration file?

cheers,  
J
