Misha Moroshko
Misha Moroshko

Reputation: 171351

Rails 3: How to display an object in a readable format?

Displaying an object like this:

Job.find(4).inspect

results in:

#<Job id: 4, job_date: "2010-10-22", address: "some address", invoice_number: "b432", client_name: "Fred", client_email: "[email protected]", client_mobile_number: "12345678", ...>

which is not very readable.

Is there a simple way to display the object like this:

id:                   4
job_date:             "2010-10-22"
address:              "some address"
invoice_number:       "b432"
client_name:          "Fred"
client_email:         "[email protected]"
client_mobile_number: "12345678"

or some other readable way ?

Upvotes: 0

Views: 1929

Answers (2)

dombesz
dombesz

Reputation: 7899

The formatting is similar if you do raise Job.find(4).to_yaml

Upvotes: 2

Alan Peabody
Alan Peabody

Reputation: 3557

Not sure where you are looking to display this, but for the console/debugger I recommend the awesome_print gem.

https://github.com/michaeldv/awesome_print

Printing a rails object example from gem readme:

ap Account.all(:limit => 2)
[
    [0] #<Account:0x1033220b8> {
                     :id => 1,
                :user_id => 5,
            :assigned_to => 7,
                   :name => "Hayes-DuBuque",
                 :access => "Public",
                :website => "http://www.hayesdubuque.com",
        :toll_free_phone => "1-800-932-6571",
                  :phone => "(111)549-5002",
                    :fax => "(349)415-2266",
             :deleted_at => nil,
             :created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
             :updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
                  :email => "[email protected]",
        :background_info => nil
    },
    [1] #<Account:0x103321ff0> {
                     :id => 2,
                :user_id => 4,
            :assigned_to => 4,
                   :name => "Ziemann-Streich",
                 :access => "Public",
                :website => "http://www.ziemannstreich.com",
        :toll_free_phone => "1-800-871-0619",
                  :phone => "(042)056-1534",
                    :fax => "(106)017-8792",
             :deleted_at => nil,
             :created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
             :updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
                  :email => "[email protected]",
        :background_info => nil
    }
]

Upvotes: 2

Related Questions