Rails 的 default console 顯示看起來有點不好閱讀、不好看、也不美麗,在開發上對於資料的顯示相對於來說就是一路到底的感覺。當顯示的資料表中的欄位比較多時,對於開發來說又更不容易些,這篇將介紹一些優化 console UI 的兩個用過的 gems。
# bundle exec rails console
User.all
User Load (0.3ms) SELECT "users".* FROM "users" /* loading for inspect */ LIMIT $1 [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: 1, name: "chester", email: "aaa@aa.com", tel: "0123456789", created_at: "2021-08-30 05:00:22.542415000 +0000", updated_at: "2021-08-30 05:00:22.542415000 +0000">, #<User id: 2, name: "chester_2", email: "bbb@bb.com", tel: "9876543210", created_at: "2021-08-30 08:00:36.836992000 +0000", updated_at: "2021-08-30 08:00:36.836992000 +0000">, #<User id: 3, name: "chester_3", email: "ccc@cc.com", tel: "543219876540", created_at: "2021-08-30 08:00:53.073957000 +0000", updated_at: "2021-08-30 08:00:53.073957000 +0000">]>
User.pluck(:name)
=> ["chester", "chester_2", "chester_3"]
也可以透過 yaml 格式來讓 format 更好看些
# bundle exec rails console
y User.first.attributes
# or
puts User.first.attributes.to_yaml
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
---
id: 1
name: chester
email: aaa@aa.com
tel: '0123456789'
created_at: !ruby/object:ActiveSupport::TimeWithZone
utc: 2021-08-30 05:00:22.542415000 Z
zone: &1 !ruby/object:ActiveSupport::TimeZone
name: Etc/UTC
time: 2021-08-30 05:00:22.542415000 Z
updated_at: !ruby/object:ActiveSupport::TimeWithZone
utc: 2021-08-30 05:00:22.542415000 Z
zone: *1
time: 2021-08-30 05:00:22.542415000 Z
=> nil
註: 各人覺得相較來看安裝底下兩個 gems 還是比較好看,使用經驗覺得都滿好用的,看團隊決定要安裝哪個都好~
# Gemfile
group :development, :test do
gem "pry-rails"
end
# bundle exec rails console
User Load (0.4ms) SELECT "users".* FROM "users"
=> [#<User:0x00007fb5a1bf1770
id: 1,
name: "chester",
email: "aaa@aa.com",
tel: "0123456789",
created_at: Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00,
updated_at: Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00>,
#<User:0x00007fb5611bb188
id: 2,
name: "chester_2",
email: "bbb@bb.com",
tel: "9876543210",
created_at: Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00,
updated_at: Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00>,
#<User:0x00007fb5611ba850
id: 3,
name: "chester_3",
email: "ccc@cc.com",
tel: "543219876540",
created_at: Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00,
updated_at: Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00>]
User.pluck(:name)
=> ["chester",
"chester_2",
"chester_3"]
# Gemfile
group :development, :test do
gem "awesome_print"
end
~/.irbrc
# ~/.irbrc
# require
require "awesome_print"
# set format in rails console
AwesomePrint.irb!
# put hacky convenience methods here
# bundle exec rails console
User.all
User Load (0.4ms) SELECT "users".* FROM "users"
[
[0] #<User:0x00007fd4c407c5b0> {
:id => 1,
:name => "chester",
:email => "aaa@aa.com",
:tel => "0123456789",
:created_at => Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00,
:updated_at => Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00
},
[1] #<User:0x00007fd4c407c4e8> {
:id => 2,
:name => "chester_2",
:email => "bbb@bb.com",
:tel => "9876543210",
:created_at => Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00,
:updated_at => Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00
},
[2] #<User:0x00007fd4c407c420> {
:id => 3,
:name => "chester_3",
:email => "ccc@cc.com",
:tel => "543219876540",
:created_at => Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00,
:updated_at => Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00
}
]
User.pluck(:name)
[
[0] "chester",
[1] "chester_2",
[2] "chester_3"
]
滿推這篇 rails console 設定來做達到更好的效果,不過有時候又覺得太麻煩了啊~還是用單一一種來處理就好,提供參考。
https://www.bruceli.net/tw/2014/08/13/awesome-rails-console-customization-using-pry.html