Forum: Ruby on Rails How to create a two dimensional table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Valentino L. (Guest)
on 2009-01-14 12:12
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
<table border="1">
<tr>
  <th></th>
<% for s in @service_code %>
  <th><%=s.col02 %></th>
<% end %>
</tr>

<% for h in @hospital_code %>
  <tr>
  <td><%=h.col01 %></td>
  </tr>
<% end %>
</table>

Many thanks
Valentino
Valentino L. (Guest)
on 2009-01-15 03:42
Dear all

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

Thank you.

Valentino
Colin L. (Guest)
on 2009-01-15 12:18
(Received via mailing list)
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>
Colin L. (Guest)
on 2009-01-15 12:18
(Received via mailing list)
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>
Rob Z. (Guest)
on 2009-01-15 16:24
(Received via mailing list)
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
--------------------------------------------
<table border="1">
  <tr>
    <th></th>
    <th>AS</th><th>BS</th><th>CS</th><th>IS</th><th>MS</th>
  </tr>

<% for srv in @services %>
  <tr>
    <td><%= srv.col01 %></td>
    <td><%= srv.AS %></td>
    <td><%= srv.BS %></td>
    <td><%= srv.CS %></td>
    <td><%= srv.IS %></td>
    <td><%= srv.MS %></td>
  </tr>
<% end %>
</table>


Hope this helps.

Rob
Saro V. (Guest)
on 2013-06-25 13:48
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
Colin L. (Guest)
on 2013-06-25 23:53
(Received via mailing list)
On 25 June 2013 10:48, Saro V. <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
This topic is locked and can not be replied to.