将一些对象从一个数据库迁移到另一个数据库

如何从一个数据库(开发,sqlite)转储一个用户与他的所有关联(评论,post等)以将其插入另一个(生产,mysql)。

我应该将它转储到yaml或sql或其他东西?

好。

God Save the YAML

我已经在开发中使用YAML转储到文件中并在我的生产中加载它。 因为它是auto_increament,所以有被改变的黑客入侵。

发展

 user = User.find X posts = user.posts comments = user.comments ... File.open("user.yml", "w") { |f| f << YAML::dump(user) } File.open("comments.yml", "w"){ |f| f << YAML::dump(comments) } File.open("posts.yml", "w") { |f| f << YAML::dump(posts) } ... 

生产

 user = YAML::load_file("user.yml") posts = YAML::load_file("posts.yml") comments = YAML::load_file("comments.yml") new_user = user.clone.save # we should clone our object, because it isn't exist posts.each do |p| post = p.clone post.user = new_user post.save end ... 

您可以使用此gem: https : //github.com/ludicast/yaml_db

YamlDb是一种与数据库无关的转储和恢复数据格式。 它补充了db / schema.rb中的数据库无关模式格式。 数据保存到db / data.yml中。