2011-04-18 97 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

相關問題