Rails 4: Export database to YAML

2020-05-23 16:49发布

问题:

With Rails 2/3 it was possible and easy to export (dump) a database to YAML using one of a few plugins (see Best way to export a database table to a YAML file?).

However, none of these plugins seem to be compatible with Rails 4. Is there still an easy way to do this?

回答1:

The method indicated in the post linked in the question is still valid, but the code is not anymore a plugin: it's the yaml_db gem.

The repository is at https://github.com/yamldb/yaml_db

To dump the db, add the gem to the Gemfile, issue

bundle update 

and then issue:

rake db:data:dump

the dump file is:

db/data.yml

If, like me, you hit the error

undefined method `[]=' for #<ActiveRecord::Result:0x007ff34c76d508>

it can be fixed by following the instructions given in this ticket:

https://github.com/ludicast/yaml_db/pull/45

All you should have to do is add

gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'

to the Gemfile