2011-04-18 94 views

回答

12

确定。

God Save the YAML

我用YAML倾倒在我的生产发展和加载该文件。有id的破解,已经改变,因为它是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 
... 
+0

注为3.1+使用'user.dup.save' – 2013-08-05 01:35:08

+1

添加到乔治·陈的评论的Rails:Rails的3.1+使用'post = p.dup' – wrydere 2014-06-10 21:54:01

2

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

YamlDb是倾销和恢复数据的数据库无关 格式。 它补充了在DB/schema.rb发现 数据库无关的架构格式 。数据是 保存到db/data.yml中。

+0

我知道这种宝石。这是非常好的,我经常使用它来倾倒和加载我的发展。但这不是我需要的。它转储整个数据库,但我只需要传输我需要的确切数据。 – fl00r 2011-04-18 09:05:50

+0

其实,我不知道联想,但这个家伙加入了表转储https://github.com/ludicast/yaml_db/pull/14。也许它会有所帮助。 – 2011-04-18 09:13:38

+0

不,我仍然需要转储确切的对象(数据库中的行)。无论如何,谢谢:)我正在倾销我的对象到YAML中。它的工作原理,但看起来蛮力我,所以我问这个问题 – fl00r 2011-04-18 09:17:16

相关问题