Mongomapper嵌入文档“无法序列化对象”错误
我对 mongodb 很陌生,我正在使用 sinatra 和 mongomapper 通过以下设置更新嵌入文档的值:
class TeamMember
include MongoMapper::Document
key :name, String, :required => true
many :team_member_projects
end
class TeamMemberProject
include MongoMapper::EmbeddedDocument
key :date, Date, :required => true
one :project
end
class Project
include MongoMapper::Document
key :name, String, :required => true
end
修饰符代码是:
team_member = TeamMember.find(params[:team_member])
project = Project.find(params[:project])
date = Date.parse(params[:date])
tm_project = TeamMemberProject.new(:project => project, :date => date)
team_member.push(:team_member_projects => tm_project)
team_member.save
但我收到 .push
行的错误:
BSON::InvalidDocument at /project/add
Cannot serialize an object of class TeamMemberProject into BSON.
我没有正确声明我的嵌入文档吗?或者还有其他方法来更新嵌入文档,我不知道。我正在尝试使用: http://mongomapper.com/documentation/plugins/modifiers。 html#push
I'm quite new to mongodb and I'm using sinatra and mongomapper to update the values of an embedded document with the following set up:
class TeamMember
include MongoMapper::Document
key :name, String, :required => true
many :team_member_projects
end
class TeamMemberProject
include MongoMapper::EmbeddedDocument
key :date, Date, :required => true
one :project
end
class Project
include MongoMapper::Document
key :name, String, :required => true
end
The modifier code is:
team_member = TeamMember.find(params[:team_member])
project = Project.find(params[:project])
date = Date.parse(params[:date])
tm_project = TeamMemberProject.new(:project => project, :date => date)
team_member.push(:team_member_projects => tm_project)
team_member.save
but I get the error for .push
line:
BSON::InvalidDocument at /project/add
Cannot serialize an object of class TeamMemberProject into BSON.
Did I not declare my embedded document properly? Or is there another way to update embedded documents, I don't know about. I'm trying to use: http://mongomapper.com/documentation/plugins/modifiers.html#push
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎有效
似乎我必须使用
project.id
。不知道为什么。也不知道为什么我的.push
不起作用,因为我认为它与<<
做同样的事情。This seems to work
It seems like I have to use
project.id
. Not sure why. Also not sure why my.push
doesn't work, as I would have assumed it does the same thing as<<
.