Reputation: 26919
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.
Upvotes: 49
Views: 33054
Reputation: 13887
Here are a few options
y your_code
gem install awesome_print
Then in irb or pry
require 'awesome_print'
ap your_code
Upvotes: 6
Reputation: 1991
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.
Upvotes: 48
Reputation: 12683
Use pry
Without pry:
2.3.1 :001 > SupplierTerm.first
SupplierTerm Load (39.4ms) SELECT "supplier_terms".* FROM "supplier_terms" ORDER BY "supplier_terms"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<SupplierTerm id: "1bc48081-402a-41d9-b6af-d783c28bb363",
entity_id: "927b398f-2bbd-40cb-b668-eb284e26688d", uses_custom_terms:
false, requires_credit_check: false, requires_identity_check: false,
requires_guarantees: true, requires_trade_reference_check: true,
minimum_guarantees: 1, minimum_trade_references: 1, trade_account_limit:
20000, created_at: "2017-02-01 22:11:49", updated_at: "2017-02-01
22:11:49", created_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6",
updated_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6", questions: [],
minimum_approvers: 1, excluded_sources: nil>
With pry:
2.3.1 :002 > pry
[1] pry(main)> SupplierTerm.first
SupplierTerm Load (0.4ms) SELECT "supplier_terms".* FROM "supplier_terms" ORDER BY "supplier_terms"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<SupplierTerm:0x007fb4e1feff40
id: "1bc48081-402a-41d9-b6af-d783c28bb363",
entity_id: "927b398f-2bbd-40cb-b668-eb284e26688d",
uses_custom_terms: false,
requires_credit_check: false,
requires_identity_check: false,
requires_guarantees: true,
requires_trade_reference_check: true,
minimum_guarantees: 1,
minimum_trade_references: 1,
trade_account_limit: 20000,
created_at: Wed, 01 Feb 2017 22:11:49 UTC +00:00,
updated_at: Wed, 01 Feb 2017 22:11:49 UTC +00:00,
created_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6",
updated_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6",
questions: [],
minimum_approvers: 1,
excluded_sources: nil>
Upvotes: 5
Reputation: 1021
There is an awesome gem called Jazz Hands. Includes pry-based enhancements, hirb, and awesome_print in rails console.
P.S. You might want to use the fork Jazz Fingers to make it compatible with Ruby 2.1.2
Upvotes: 2
Reputation: 2156
If you don't want to use a gem, here's the low rent version:
puts User.all.to_yaml
Upvotes: 30
Reputation: 10395
You could try the awesome_print gem : https://github.com/michaeldv/awesome_print
Once installed, you can pretty print any object using :
ap User.all
Upvotes: 8