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
On Jan 15, 8:21 pm, "Colin Law" <[email protected]> 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 <[email protected]>
>
>
>
> > 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---