添加到 MongoMapper 中现有的 EmbeddedDocuments
获取以下 MongoMapper 文档。
class Schedule
include MongoMapper::Document
key :name, String
key :description, String
key :active, Boolean
many :periods
timestamps!
userstamps!
end
class Period
include MongoMapper::EmbeddedDocument
key :number, Integer
key :descriptor, String
key :begin, Time
key :end, Time
end
另外,采用以下 Padrino 路线。
post :period, :map => '/schedule/period' do
s = Schedule.first(params[:id])
s.periods = [
:number => 1,
:descriptor => "This is a description.",
:begin => Time.now,
:end => Time.now
]
end
但是,如果我的日程表中已经有多个periods
,我是否会覆盖现有的periods
?我怎样才能避免这种情况?
Take the following MongoMapper documents.
class Schedule
include MongoMapper::Document
key :name, String
key :description, String
key :active, Boolean
many :periods
timestamps!
userstamps!
end
class Period
include MongoMapper::EmbeddedDocument
key :number, Integer
key :descriptor, String
key :begin, Time
key :end, Time
end
Also, take the following Padrino routing.
post :period, :map => '/schedule/period' do
s = Schedule.first(params[:id])
s.periods = [
:number => 1,
:descriptor => "This is a description.",
:begin => Time.now,
:end => Time.now
]
end
But, if I already have several periods
within the schedule, won't I just be overwriting the existing periods
? How can I avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,mongomapper.com 上尚未记录关联方法。但是...
使用 concat 运算符,它是在关联上定义的:
您可以将其传递为哈希或文档。
Alas, association methods haven't been documented yet on mongomapper.com. But...
Use the concat operator, which is defined on associations:
You can hand it either a Hash or a document.