3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsコンソールでGemを使わずにデータを見やすく表示する

Last updated at Posted at 2019-04-19

はじめに

Railsチュートリアル実施中の初心者です。
データベース内のちょっとしたデータの確認などにはRailsコンソールを利用しますが、1レコードが一行で表示されるため、少々見にくいです。
データを見やすくするためのGemはいくつかありますが、Gemに頼らず、見やすくできないか試行錯誤しました。

普通に表示

> User.first
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2019-04-16 16:22:28", updated_at: "2019-04-19 03:08:30", password_digest: "$2a$10$fN.8W9WjbZ9Nud5.Vzf7VOAxXd10K9gOYbf6g2vfXws...", remember_digest: nil, admin: true, activation_digest: "$2a$10$NaAL98dZkFHwse09sO9bWutaxjwIofiSvNUKVexHSnd...", activated: true, activated_at: "2019-04-16 16:22:28", reset_digest: "$2a$10$FJ6/086gEWuo1Urcos6bKuldLl4p.3UtRkcZYKnyaAr...", reset_sent_at: "2019-04-19 03:08:30">

見やすく表示

puts User.first.inspect.gsub(/,/,"\n")

#<User id: 1
 name: "Example User"
 email: "example@railstutorial.org"
 created_at: "2019-04-16 16:22:28"
 updated_at: "2019-04-19 03:08:30"
 password_digest: "$2a$10$fN.8W9WjbZ9Nud5.Vzf7VOAxXd10K9gOYbf6g2vfXws..."
 remember_digest: nil
 admin: true
 activation_digest: "$2a$10$NaAL98dZkFHwse09sO9bWutaxjwIofiSvNUKVexHSnd..."
 activated: true
 activated_at: "2019-04-16 16:22:28"
 reset_digest: "$2a$10$FJ6/086gEWuo1Urcos6bKuldLl4p.3UtRkcZYKnyaAr..."
 reset_sent_at: "2019-04-19 03:08:30">
=> nil
3
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?