More Fun with Ruport and Ruby on Rails

Posted by acts_as_flinn Fri, 09 Mar 2007 07:35:00 GMT

So I’ve been doing some initial work on reporting in an app I am working on. The following code will run an sql query stored in an ActiveRecord model, then use a Ruport dataset to send output to the web browser. Options other than hmtl are csv, pdf, and text.

@report = Report.find(params[:id])

@results = ActiveRecord::Base.connection.select_all(@report.sql_query)

if @results.size > 0
  @ds = Ruport::Data::Table.new(:data => @results, :column_names => @arr.first.keys)
  @ds.column_names = @ds.column_names.collect{ |c| c.titleize }

  render :layout => ‘report’, :text => @ds.as(:html)
else
  render :layout => ‘report’, :text => ‘Empty Report’
end

Very nice. I will likely be combining this with Gruff in some capacity.


ss_blog_claim=746d258dc975cb7923cc57154dbf1d71