在Rails控制台中每行显示一个结果

时间:2014-07-30 07:45:12

标签: ruby-on-rails

是否可以显示rails c查询的结果,并将每个结果放在一个单独的行上,而不仅仅是所有内联?

作为我的意思的例子,采用普通的mac终端列表文件命令。区别于:

$ ls
CNAME               README.md           _layouts            config.rb           index.html          package.json            stylesheets
Gemfile             _config.yml         _posts              contact.html            js              rss.xml
Gemfile.lock            _drafts             _site               copyright.html          scss
Gruntfile.js            _includes           assets              node_modules            sitemap.xml

ls -l 总计120

-rw-r--r--   1 Luke  staff   140 12 Mar 22:45 Gemfile
-rw-r--r--   1 Luke  staff   637 12 Mar 22:45 Gemfile.lock
-rw-r--r--   1 Luke  staff  2312 12 Mar 22:45 Gruntfile.js
-rw-r--r--   1 Luke  staff   996  4 Jan  2014 README.md
-rw-r--r--   1 Luke  staff   317  4 Jan  2014 _config.yml
drwxr-xr-x   7 Luke  staff   238 17 Feb 09:56 _drafts
drwxr-xr-x  15 Luke  staff   510 14 Jul 18:28 _includes
drwxr-xr-x   6 Luke  staff   204  6 Apr 19:32 _layouts
drwxr-xr-x   9 Luke  staff   306 14 Jul 18:29 _posts
drwxr-xr-x  18 Luke  staff   612 14 Jul 18:29 _site
drwxr-xr-x  11 Luke  staff   374  4 Jan  2014 assets
-rw-r--r--   1 Luke  staff   930 12 Mar 22:45 config.rb
-rw-r--r--   1 Luke  staff   725  6 Apr 19:34 contact.html
-rw-r--r--   1 Luke  staff  1819  6 Apr 19:34 copyright.html
-rw-r--r--   1 Luke  staff  1892 12 Mar 22:45 index.html
drwxr-xr-x   7 Luke  staff   238 12 Mar 22:45 js
drwxr-xr-x  16 Luke  staff   544  6 Jan  2014 node_modules
-rw-r--r--   1 Luke  staff   536 12 Mar 22:45 package.json
-rw-r--r--   1 Luke  staff   891  4 Jan  2014 rss.xml
drwxr-xr-x  26 Luke  staff   884  6 Apr 19:34 scss
-rw-r--r--   1 Luke  staff  1717  4 Jan  2014 sitemap.xml
drwxr-xr-x   5 Luke  staff   170  6 Apr 19:34 stylesheets

1 个答案:

答案 0 :(得分:0)

编辑:我试着更明确。

你可以尝试pry而不是irb控制台,pry是一个具有很多功能的替代ruby shell(详情请看这里:http://pryrepl.org/)。其中一个功能是格式化结果输出。有了这个,一个项目逐个出现。

示例:没有pry

2.1.1 :001 > User.all
  User Load (7.7ms)  SELECT "users".* FROM "users"
 => #<ActiveRecord::Relation [#<User id: 1, first_name: "Foo", last_name: "Bar", created_at: "2014-07-24 14:33:34", updated_at: "2014-07-24 14:33:34">, #<User id: 2, first_name: "Bar", last_name: "Foo", created_at: "2014-07-24 14:33:34", updated_at: "2014-07-24 14:33:34">, #<User id: 3, first_name: "Bar", last_name: "Bar", created_at: "2014-07-24 14:35:34", updated_at: "2014-07-24 14:35:34">]>

与撬:

[1] pry(main)> User.all
  User Load (4.3ms)  SELECT "users".* FROM "users"
=> [#<User id: 1, first_name: "Foo", last_name: "Bar", created_at: "2014-07-24 14:33:34", updated_at: "2014-07-24 14:33:34">,
 #<User id: 2, first_name: "Bar", last_name: "Foo", created_at: "2014-07-24 14:33:34", updated_at: "2014-07-24 14:33:34">
 #<User id: 3, first_name: "Bar", last_name: "Bar", created_at: "2014-07-24 14:35:34", updated_at: "2014-07-24 14:35:34">]>
[2] pry(main)>

您可以轻松安装:将pry-rails添加到您的Gemfile

gem 'pry-rails', :group => :development

并制作bundle install

启动控制台

rails console

希望这有帮助