添加到 MongoMapper 中现有的 EmbeddedDocuments

发布于 2024-11-13 12:07:14 字数 732 浏览 3 评论 0原文

获取以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2024-11-20 12:07:14

遗憾的是,mongomapper.com 上尚未记录关联方法。但是...

使用 concat 运算符,它是在关联上定义的:

s.periods << {
  :number => 1,
  :descriptor => "This is a description.",
  :begin => Time.now,
  :end => Time.now
}

您可以将其传递为哈希或文档。

Alas, association methods haven't been documented yet on mongomapper.com. But...

Use the concat operator, which is defined on associations:

s.periods << {
  :number => 1,
  :descriptor => "This is a description.",
  :begin => Time.now,
  :end => Time.now
}

You can hand it either a Hash or a document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文