这是命令
a = Post.first
b = Category.last
a.category = b
b.save <-- return true but no persist on db
当我尝试更改嵌入对象的单个值时,说RuntimeError:无法修改冻结的BSON :: Document。
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 :(得分:0)
a = Post.first
b = Category.last
a.category = b
b.save <-- return true but no persist on db
您应该保存变量a
而不是b
,如下所示:
a.category = b
a.save #this would save the category id to post.
现在,
a.category.name = "test"
会奏效。