【发布时间】:2014-01-12 07:26:43
【问题描述】:
当我运行 Rails 控制台时,如何在单独的行中显示每个项目?而不是
> Post.all
=> #<ActiveRecord::Relation [#<Post id: 1, title: "Post #0", comment: nil, link: "http://yahoo.com", user_id: 1, created_at: "2013-09-30 02:29:28", updated_at: "2013-09-30 02:29:28">, #<Post id: 2, title: "Post #1", comment: nil,...
它会显示为
> Post.all
=> #<ActiveRecord::Relation [
#<Post id: 1, title: "Post #0", comment: nil, link: "http://yahoo.com", user_id: 1, created_at: "2013-09-30 02:29:28", updated_at: "2013-09-30 02:29:28">,
#<Post id: 2, title: "Post #1", comment: nil,...
类似于 Perl 调试器中的x。我试过了
Post.all.each{|e| e.inspect + "\n"}
但这只会让事情变得更糟,而且不是很方便。
我看到了Ruby on Rails: pretty print for variable.hash_set.inspect ... is there a way to pretty print .inpsect in the console? 和https://github.com/michaeldv/awesome_print
但这似乎不起作用
irb(main):005:0> require "awesome_print"
=> false
irb(main):006:0> ap Post.all
#<ActiveRecord::Relation [#<Post id: 1, title: "Post #0",
【问题讨论】:
-
请注意,
Post.all不会返回一个数组,而是一个ActiveRecord::Relation。对于真正的Array,您需要#to_a。
标签: ruby-on-rails ruby rails-console