# Having an integer matrix inside model

**URL:** <https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059>\
**Category:** Rails\
**Created:** [May 15, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059 "2009-05-15T12:05:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 15, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/1 "2009-05-15T12:05:25Z")

</div>

Hey,

Is there any way I can have a model to store an N by N integer matrix?  
Something that would be able to keep record of rows and seats.

n[i][j] = 1 if the seat is empty  
n[i][j] = 2 if the seat is reserved

---

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 15, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/2 "2009-05-15T14:52:36Z")

</div>

Possibly  
Auditorium has\_many Rows  
Row has\_many Seats  
Seat belongs\_to Row  
Row belongs\_to Auditorium

Colin

2009/5/15 pb pb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 16, 2009, 12:36am UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/3 "2009-05-16T00:36:03Z")

</div>

too much complex:)

is good enough to keep only row#nr and seat#nr, eg

table seats contains:  
id  
seat\_number  
row\_number

is much faster to search/update

tom

Colin L. wrote:

> >

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

- experienced RoR/PHP freelancer, available for hire

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 16, 2009, 11:00am UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/4 "2009-05-16T11:00:11Z")

</div>

Colin L. wrote:

> You might find the arrangement using relationships is actually  
> significantly  
> simpler to develop, and with a maximum of a few thousand seats  
> (presumably)  
> you are unlikely to notice any difference in lookup.
> 
> The fact that you talk about rows suggests that this is a concept that  
> is  
> part of your mental image of the problem, it is often best to map your  
> mental image of the problem into the application, it makes it a better  
> representation of the real world, which may be much more important than  
> a  
> few milliseconds of database lookup.
> 
> For example, do you wish to keep the number of seats in each row  
> somewhere?  
> Put it in the rows table.
> 
> Colin
> 
> 2009/5/15 Tom Z Meinlschmidt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Well, each row will have the same amount of possible seats. I want to  
store 1 if there’s a seat, or 0 if there’s something else, like an alley  
or some obstacle.  
a room plan wood look like

111111111  
111110111  
111110111  
111110111  
111110111  
111110111

222222222

where the 1 signifies a seat, 0 signifies stairs and 2 is the screen.  
I came up with a model

I have a class room which contains a name:string, rows:integer,  
seats:integer  
and then I have roomplan which has room\_id:integer , row:integer,  
seat:integer, value:integer to keep those 1s and 0s…

What do you think?

---

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 16, 2009, 11:32am UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/5 "2009-05-16T11:32:46Z")

</div>

I would still go for Models Room, Row and Seat with the relationships as  
above, it seems like a better mapping of the problem. But if your  
solution  
is a better mapping for the way you see the problem then that is the  
best  
solution for you.

Consider how to access a particular seat. In the Room, Row, Seat  
solution if  
you have the record for a particular room then value for seat 5 on row 3  
would be referenced by  
room.row[3].seat[5].value (assuming zero based indexing, you might  
have to  
add one to the indexes for real world mapping)  
How would this be achieved with the Room and RoomPlan models?

Colin

2009/5/16 pb pb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 16, 2009, 9:41am UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/6 "2009-05-16T09:41:18Z")

</div>

You might find the arrangement using relationships is actually  
significantly  
simpler to develop, and with a maximum of a few thousand seats  
(presumably)  
you are unlikely to notice any difference in lookup.

The fact that you talk about rows suggests that this is a concept that  
is  
part of your mental image of the problem, it is often best to map your  
mental image of the problem into the application, it makes it a better  
representation of the real world, which may be much more important than  
a  
few milliseconds of database lookup.

For example, do you wish to keep the number of seats in each row  
somewhere?  
Put it in the rows table.

Colin

2009/5/15 Tom Z Meinlschmidt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![p-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a3d4f5/32.png) [@p-b](https://www.ruby-forum.com/u/p-b)\
**Post date:** [May 16, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/having-an-integer-matrix-inside-model/168059/7 "2009-05-16T15:07:27Z")

</div>

Colin L. wrote:  
[…]

> if  
> you have the record for a particular room then value for seat 5 on row 3  
> would be referenced by  
> room.row[3].seat[5].value (assuming zero based indexing, you might  
> have to  
> add one to the indexes for real world mapping)  
> How would this be achieved with the Room and RoomPlan models?
> 
> Colin

Well, another way to do it might be to simply have a 2D array serialized  
to YAML. That gives the addressability without the join overhead. I  
tend to agree with you, though: a separate Seat table (probably not a  
Row table, though) would be good – it would allow the storage of more  
than 1 byte of data for each seat…

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
