Exporting ActiveRecords as yaml


#1

Hi,

I have a number of .yml fixtures containing data that is loaded into my
db as a migration. Some of this data needs to be cleaned up, so I am
using the admin part of my rail app to do this editing.

I am now hoping to export the edited data back into yaml, so that I can
update my fixture data and drop and recreate the db as I need.

When I do something like:

Colour.find(:all).to_yaml

I get output like:

  • !ruby/object:Colour
    attributes:
    name: Black
    id: “1”
  • !ruby/object:Colour
    attributes:
    name: Blue
    id: “2”

I am hoping to formal the yaml in the same way as the fixtures:

black:
id: 1
name: ‘Black’
blue:
id: 2
name: ‘Blue’

Is this possible or do I have to format the output myself?

Thanks,
GiantCranes


#2

Check this plugin out:

http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-
reference-data

Careful when you use it; it overwrites your text fixtures.

You might want to just use part of the plugin’s code to only do the
to yaml bit instead. Have a look here:

http://topfunky.net/svn/plugins/ar_fixtures/lib/ar_fixtures.rb

-christos