[LRUG] Best practice for reports

Paul Doerwald paul at liquidmedia.ca
Mon May 21 08:53:45 PDT 2007


Hi everyone,

I'm asking this question in the interests of separating business  
logic from presentation. I have a standard reporting problem — I have  
a bunch of data in a database, and I want to dig through it, sort it,  
sum it, frob it, average it, and display it.

The easiest way to do it is in the view:

<th><%= f.name</th><td><%= Foo.count(:conditions => "blah") %></td>

but then I've committed the heinous sin of putting the business  
(reporting!) logic into the view. I could avoid that by stuffing it  
into the controller, essentially mimicking the table structure, but  
in a hash of some sort:

report = Hash.new
report["Baz"] = { :name => f.name, :sum => Foo.count(:conditions =>  
"blah") }

But I'm not convinced that I'm winning anything here.... plus the  
"Rails" way of doing things is to move the logic out of the  
controller into the model... but then what if I'm pulling data from  
half a dozen different tables? Presumably then I need to create a new  
(non-ActiveRecord) model, perhaps:

class Report
   class self.main_report
     # create the hash here
   end
end

But that is just indirection another step away, so what I really  
should do is:

class Report
end

class MainReport < Report
   attr_accessor :title, ...all the other fields I might want to use...

   def frob
     # prepare the report
   end
end

and then in the controller, instantiate the MainReport, and do the  
looping in the view... but now I've created a whole lot of work for  
myself to simply do what I could do with much less effort in the  
first instance.

I suspect the last option is probably the best — at least of the ones  
I've presented — but I'm more interested in what others have read  
about or done which they feel is the "best practice" for reporting.

Along this thread, has anyone used http://rubyreports.org/ ? Is it  
appropriate for me? I haven't looked closely yet, but am about to.

Thanks,
Paul.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2421 bytes
Desc: not available
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20070521/2bcb95c8/attachment-0004.bin>


More information about the Chat mailing list