我如何从一个数据库(开发、sqlite)转储一个用户及其所有关联(评论、帖子等)以将其插入另一个数据库(生产、mysql)。
我应该将其转储到 yaml 或 sql 或其他东西中?
Ok.
God Save the YAML
我已经使用 YAML 从开发中转储到文件中,并将其加载到我的生产中。有 id 的 hack,由于它是 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
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)