UPDATE Now packaged as a plugin! Makes fixtures also. And, is MySQL friendly! UPDATE
Sometimes you need to save data and use it again when you deploy a server or continue a project on a different machine.
I wrote a plugin for ActiveRecord that lets you do this easily.
It adds two class methods that let you dump_to_file and load_from_file. The contents are written to the ‘db’ directory with the table name as the filename. When you call Model.load_from_file, existing data in that table is deleted and reloaded, but primary key id’s are kept intact.
An additional Model.to_fixture method makes fixtures from existing data. It writes a file to ./test/fixtures/models.yml that can be loaded normally in your unit and functional tests.
Install with the new plugin script. (Included in Rails 1.0).
./script/plugin install http://topfunky.net/svn/plugins/ar_fixtures
Use it in code, or use the built-in ‘runner’ command.
# Dump user table to db/users.yml ./script/runner "User.dump_to_file" # Or ./script/runner "User.to_fixture"
# Read it back into the database ./script/runner "User.load_from_file"