MySQL-Style Output for the Rails Console

While poking around the database in my rails console, I often found myself jumping to the mysql console just so I could get an easier-to-digest view of the data. I finally had enough of this silliness and wrote up a quick function to dump of set of ActiveRecord objects in the mysql report format.

>> report(records, :id, :amount, :created_at)
+------+-----------+--------------------------------+
| id | amount | created_at |
+------+-----------+--------------------------------+
| 8301 | $12.40 | Sat Feb 28 09:20:47 -0800 2009 |
| 6060 | $39.62 | Sun Feb 15 14:45:38 -0800 2009 |
| 6061 | $167.52 | Sun Feb 15 14:45:38 -0800 2009 |
| 6067 | $12.00 | Sun Feb 15 14:45:40 -0800 2009 |
| 6059 | $1,000.00 | Sun Feb 15 14:45:38 -0800 2009 |
+------+-----------+--------------------------------+
5 rows in set

Grab it from GitHub and stick it in your .irbrc.

Tweet