Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making the rails console output a little more pretty

a rails console output looks like this:

User.all
=> [#<User id: 1, name: "Michael Hartl", email: "[email protected]",
created_at: "2011-12-05 00:57:46", updated_at: "2011-12-05 00:57:46">,
#<User id: 2, name: "A Nother", email: "[email protected]", created_at:
"2011-12-05 01:05:24", updated_at: "2011-12-05 01:05:24">]

I was wondering if there is command that can make it easier to read? for example there was a .pretty command in MongoDB console that was formatting the output a little more eye friendly. But not sure if there is something similar in Rails or not.

like image 501
Bohn Avatar asked Jan 16 '13 21:01

Bohn


4 Answers

A bit more elegant shorthand:

y User.all
like image 155
valk Avatar answered Nov 03 '22 12:11

valk


I've been using pp. The pp stands for "pretty print." No Gem required.

On rails console try doing this:

pp User.all

You'll get each attributes and their value in the record display in a row instead a bundle of them if you simply do User.all.

Here's the documentation:

https://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html

I am using Rails 5.1.3 and ruby 2.4.1p111 and it came already installed in my project. If this don't work, I imagine you have to do require 'pp'. I hope this helps.

like image 25
myhouse Avatar answered Nov 03 '22 12:11

myhouse


If you don't want to use a gem, here's the low rent version:

 puts User.all.to_yaml
like image 30
Ben Avatar answered Nov 03 '22 11:11

Ben


Also you could use this incredible gem:

Awesome Print

like image 11
Leszek Andrukanis Avatar answered Nov 03 '22 12:11

Leszek Andrukanis