Friday, March 09, 2007

For yarn.sness.org, I need to slurp a bunch of data into a database,
and I remembered doing this for lovemygarden as well. The trick is to
put the data in a yaml file and use load_from_file to load the data.

First, install the plugin:


./script/plugin install http://topfunky.net/svn/plugins/ar_fixtures


Then, for example, if you have the following data in a file:


---
- !ruby/object:Amendment
attributes:
updated_at: 2006-07-07 11:56:10.086189
id: "1"
entry: Added 1 big bag of peat moss
user_id: "1"
created_at: 2006-04-16 12:40:00
- !ruby/object:Amendment
attributes:
updated_at: 2006-07-07 11:56:10.295888
id: "2"
entry: Added 3 shovel fulls of compost
user_id: "1"
created_at: 2006-04-18 12:40:00


You could put the following into a file in the db/migrate directory, like db/migrate/insert_data_001.rb


Amendment.load_from_file


And then run it with:


script/runner db/migrate/insert_data_001.rb



I learned this from:

nubyonrails - dump-or-slurp-yaml-reference-data