2014-08-31 40 views
0

我試圖更新mongoid上的嵌入對象和終端說真,但更改不會持久數據庫上。無法更新或銷燬mongoid上的嵌入對象

這是命令

a = Post.first 
b = Category.last 
a.category = b 
b.save <-- return true but no persist on db 

當我試圖改變嵌入對象的一個​​單值RuntimeError說:不能修改凍結BSON ::文檔。

a.category.name = "test" <-- return RuntimeError: can't modify frozen BSON::Document. 

任何想法?我使用mongoid 4.0

帖子:

class Post 
    include Mongoid::Document 

    field :name, type: String 
    field :intro, type: String 
    field :content, type: String 

    embeds_one :category 

類別:

class Category 
    include Mongoid::Document 

    field :name, type: String 

問候,

+0

「Post」和「Category」是什麼樣的?請記住,嵌入對象實際上只是包含在一些Mongoid中的'Hash'字段(或它們的數組),所以單獨保存嵌入對象是沒有意義的,您必須保存父對象。 – 2014-08-31 17:39:04

+0

添加帖子和類別模型以首先發布。你是指什麼保存家長?謝謝 – Kerm1t 2014-08-31 17:56:25

回答

0
a = Post.first 
b = Category.last 
a.category = b 
b.save <-- return true but no persist on db 

你應該保存可變的,而不是B,這樣的:

a.categor y = b

a.save #this將保存要發佈的類別ID。

而現在,

a.category.name = "test" 

會工作。