Skip to content

Rails Quick Tip: YAML output in the Rails console

You can file this in the “things I wish I’d known about years ago” pile along with the bundle open command…

If you want to dump the contents of an object on the Rails console in a nice and readable YAML format, just use the function ‘y

E.g.

> y Company.first
  Company Load (0.3ms)  SELECT "companies".* FROM "companies" LIMIT 1
--- !ruby/ActiveRecord:Company
attributes:
  id: 2
  name: Abcam
  created_at: '2012-05-31 10:58:57.070819'
  updated_at: '2012-06-27 10:47:44.662756'
  url: http://Abcam.example.com/
  state: hidden

Simple.

I’ve no idea how it’s taken me this long to find out about it, but I’m glad I finally have.