A few days ago, Courtenay of Caboose posted an article called ‘pretty tables for ruby objects‘ that give a MySQL-command-line-client style textual view of data stored in your Rails database. The syntax worked like this:
puts Invoice.pretty_table(nil,{:max_width => 50, :line_spacer => ‘-’, :find => { :order => ‘created_at desc’ }})
For an output like so:
| created_at | updated_at | name | due_at | p |
—————————————————————————————————————
| Fri Jun 09 14:17:00 PDT 2006 | Fri Jun 09 18:34:55 PDT 2006 | Website | Sun Jul 09 14:17:00 PDT 2006 | 1 |
| Fri Jun 09 14:01:00 PDT 2006 | Fri Jun 09 18:34:55 PDT 2006 | May | Fri Jun 23 14:01:00 PDT 2006 | 1 |
| Sat Jun 03 12:25:00 PDT 2006 | Fri Jun 09 18:34:55 PDT 2006 | Invoice 3 | Mon Jul 03 12:25:00 PDT 2006 | 3 |
It didn’t make sense to me and I suggested developing a version that worked in this way instead:
Something.find(:all, :conditions => ‘whatever’).pretty_print
This way you can use your regular model accessors and simply use the ‘pretty’ output on any result set whatsoever. Read More