How to Dump a Rails Model to a YAML File

I've been wanting to do some work for a while on the UI for our performance monitoring system. But the way the performance monitoring system works, it's difficult to create fake data for development.

The solution? Jump into a Rails console on our staging server and dump a few choice records to YAML files. It's an easy trick:

item = MyItem.find(...)
File.open("sample_item.yml", "w"){ |o| o.write(item.to_yaml)  }

Now you can transfer the file to your computer and load the yaml into a model like so:

item = YAML.load_file("path/to/sample_item.yml")

Level up your software development career

Join our community of kick-ass developers as we learn engineering, DevOps, cloud architecture, and bootstrapping remote software companies. Tell me more →

We're Honeybadger. We'll never send you spam; we will send you cool shit like exclusive content, memes, and special swag.