问题描述
使用 Rails 2/3,可以使用几个插件之一轻松地将数据库导出(转储)到 YAML(请参阅 将数据库表导出到 YAML 文件的最佳方式?).
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?).
但是,这些插件似乎都不兼容 Rails 4.还有一种简单的方法可以做到这一点吗?
However, none of these plugins seem to be compatible with Rails 4. Is there still an easy way to do this?
推荐答案
问题中链接的帖子中指出的方法仍然有效,但代码不再是插件:它是 yaml_db gem.
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.
存储库位于 https://github.com/yamldb/yaml_db
要转储数据库,请将 gem 添加到 Gemfile,发出
To dump the db, add the gem to the Gemfile, issue
bundle update
然后发出:
rake db:data:dump
转储文件是:
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
你应该做的就是添加
gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'
到 Gemfile
这篇关于Rails 4:将数据库导出到 YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!