如何显示ActiveRecords,如
>> 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
我在railscasts.com/episodes/189-embedded-association和 http://asciicasts.com/episodes/189-embedded-association
答案 0 :(得分:12)
答案是“hirb”宝石,你应该能够做到
sudo gem install hirb
然后调用脚本/控制台后运行
=> require 'hirb'
=> Hirb.enable
将会执行此操作,hirb将拦截正在显示的所有ActiveRecord模型并以表格格式显示。同样在那段Railscasts中他使用了这个命令
=> ActiveRecord::Base.logger = Logger.new(STDOUT)
它会覆盖ActiveRecord的默认记录器并使其输出运行到控制台的sql命令,必须使用此命令作为脚本/控制台irb会话中使用的第一个命令,否则它将无效。我在调试时总是使用这两种方法,或只是确保一切正常。希望有所帮助。