Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show ActiveRecord objects like table in ./script/console

How to display ActiveRecords like

  >> Role.all
  +----+-----------+-------------------------+-------------------------+
  | id | name      | created_at              | updated_at              |
  +----+-----------+-------------------------+-------------------------+
  | 1  | Admin     | 2009-11-16 21:22:59 UTC | 2009-11-16 21:22:59 UTC |
  | 2  | Moderator | 2009-11-16 21:23:06 UTC | 2009-11-16 21:23:06 UTC |
  | 3  | Author    | 2009-11-16 21:23:16 UTC | 2009-11-16 21:23:16 UTC |
  +----+-----------+-------------------------+-------------------------+
  3 rows in set

?

I saw it in railscasts.com/episodes/189-embedded-association and http://asciicasts.com/episodes/189-embedded-association

like image 655
Vlad Avatar asked Dec 17 '09 18:12

Vlad


1 Answers

the answer is the "hirb" gem, you should just be able to do

sudo gem install hirb

then after calling script/console run

=> require 'hirb'
=> Hirb.enable

that will do it, hirb will intercept all of the ActiveRecord models being displayed and display in the table format. Also in that episode of Railscasts he uses this command

=> ActiveRecord::Base.logger = Logger.new(STDOUT)

that overrides the default logger for ActiveRecord and makes it output the sql commands it runs to the console, you MUST use this command as the very first command you use in the script/console irb session or it won't work. I use both of these all of the time when debugging or just making sure things are working okay. Hope that helps.

like image 54
jacortinas Avatar answered Nov 10 '22 01:11

jacortinas