# How to create a two dimensional table

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675>\
**Category:** Rails\
**Created:** [January 14, 2009, 11:12am UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675 "2009-01-14T11:12:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 14, 2009, 11:12am UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/1 "2009-01-14T11:12:35Z")

</div>

Dear all

I have the following data in a table and I want to create a  
2-dimensional table to display it.

Table: services  
col01 col02 col03  
AH AS 1  
AH CS 2  
AH MS 3  
BH BS 1  
BH CS 1  
BH MS 1  
CH IS 1  
CH BS 2  
CH MS 1  
DH CS 1  
DH MS 1

I want to have this present on web

```
 AS BS CS IS MS

```

AH 1 2 3  
BH 1 1 1  
CH 2 1 1  
DH 1 1

I only have the idea to display the column and row title. Can anyone  
give me some idea how to generate this dynamically?. Thanks

controllers/dashboard\_controller.rb  
def main  
@hospital\_code = Service.find\_by\_sql(“select distinct hospital\_code  
from services”)  
@service\_code = Service.find\_by\_sql(“select distinct service\_code  
from services”)  
end

views/dashboard/main.html.erb

\<% for s in @service\_code %\> \<% end %\>

\<% for h in @hospital\_code %\>

\<% end %\>

| | \<%=s.col02 %\> |
| --- | --- |
| \<%=h.col01 %\> |

Many thanks  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 2:42am UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/2 "2009-01-15T02:42:50Z")

</div>

Dear all

Would you give me some hints on this? I don’t know how to do…

Thank you.

Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 11:18am UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/3 "2009-01-15T11:18:08Z")

</div>

I would suggest building a two dimensional array @values[][] in the  
controller, iterating through the records and filling in the cells.  
This can then be displayed as rows and columns in the view. I suspect  
there may be more elegant ways to do this in Ruby however.  
Colin

On Jan 14, 10:12 am, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/4 "2009-01-15T15:24:54Z")

</div>

You can create a sql query that returns the data formatted the way you  
want :

## MODEL

class Service \< ActiveRecord::Base

def self.getResults  
self.find\_by\_sql(“SELECT col01,  
sum(if(col02=‘AS’,col03, null )) as ‘AS’,  
sum(if(col02=‘BS’,col03, null )) as ‘BS’,  
sum(if(col02=‘CS’,col03, null )) as ‘CS’,  
sum(if(col02=‘IS’,col03, null )) as ‘IS’,  
sum(if(col02=‘MS’,col03, null )) as ‘MS’  
from services group by col01”)  
end

end

## CONTROLLER

def index  
@services = Service.getResults

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @services }
end

```

end

## VIEW

\<% for srv in @services %\>

\<% end %\>

| | AS | BS | CS | IS | MS |
| --- | --- | --- | --- | --- | --- |
| \<%= srv.col01 %\> | \<%= srv.AS %\> | \<%= srv.BS %\> | \<%= srv.CS %\> | \<%= srv.IS %\> | \<%= srv.MS %\> |

Hope this helps.

Rob

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 11:18am UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/5 "2009-01-15T11:18:09Z")

</div>

I would suggest building a two dimensional array @values[][] in the  
controller, iterating through the records and filling in the cells. This  
can  
then be displayed as rows and columns in the view. I suspect there may  
be  
more elegant ways to do this in Ruby however.  
Colin

2009/1/15 Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 25, 2013, 11:48am UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/6 "2013-06-25T11:48:38Z")

</div>

If the content of Col1 and Col2 is dynamic?

I have this

## MODEL

class Forecast \< ActiveRecord::Base

belongs\_to :employee  
belongs\_to :prefshift

attr\_accessible :employee\_id, :giorno, :prefshift\_id

## end

## MODEL

class Prefshift \< ActiveRecord::Base

has\_one :forecast

has\_many :employee\_prefshifts, :dependent =\> :destroy  
has\_many :employees, through: :employee\_prefshifts, :dependent =\>  
:destroy

attr\_accessible :cod, :descr, :fine, :inizio, :nome, :stato,  
:ggsettimana\_ids

def self.getCods  
self.find\_by\_sql(“SELECT id,cod FROM prefshifts WHERE stato=true  
ORDER BY inizio ASC”)  
end

## end

## MODEL

class Employee \< ActiveRecord::Base

has\_one :forecast

has\_many :employee\_prefshifts, :dependent =\> :destroy  
has\_many :prefshifts, through: :employee\_prefshifts, :dependent =\>  
:destroy

attr\_accessible :acceso, :cognome, :contract\_id, :dataAssunzione,  
:malattia, :matricola, :nome, :prefshift\_ids

## after\_initialize do self.acceso ||= true end end

and i want to have this present on web

## VIEW

```
             01/07/2013 | 02/07/2013 | 03/07/2013 | ....

```

prefshift1 employee3 employee1  
prefshit2 employee8 employee5  
prefshift3 employee6  
prefshift4 employee3 employee1 employee5  
…

* * *

Instead of prefshift1,prefshift2 etc i want display the prefshift.cod

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 25, 2013, 9:53pm UTC](https://www.ruby-forum.com/t/how-to-create-a-two-dimensional-table/157675/7 "2013-06-25T21:53:43Z")

</div>

On 25 June 2013 10:48, Saro V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If the content of Col1 and Col2 is dynamic?

Did you realise that you have replied to a thread from 2009? I hope  
the OP has not been waiting all this time for an answer.

Colin
