Re: How to create a two dimensional table

robokos
2009-01-15T14:24:03Z

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
--------------------------------------------




AS BS CS IS MS


<% for srv in @services %>

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

<% end %>



Hope this helps.

Rob


On Jan 15, 8:21 pm, "Colin Law"
<clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
wrote:
> 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 Lun
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
>
>
>
> > Dear all
>
> > Would you give me some hints on this? I don''t know how to
do...
>
> > Thank you.
>
> > Valentino
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com