编辑 MongoMapper 文档

发布于 2024-11-14 07:28:28 字数 357 浏览 2 评论 0原文

在 MongoMapper 文档中我找不到任何实际编辑文档的方法。我在其他地方也找不到任何东西。我能找到的唯一方法是这种方法:

class User
  include MongoMapper::Document

  key :name, String
end

user = User.create( :name => "Hello" )
user.name = "Hello?"

puts user.name # => Hello?

有更简单的方法吗?我知道在 DataMapper 中,我可以一次编辑多个键(或属性,在 DM 的情况下),但使用 MM 时,我一次只能编辑一个。

我错过了什么吗?

Nowhere in the MongoMapper documentation can I find any methods for actually editing documents. I can't find anything elsewhere, either. The only way I could find, is this method:

class User
  include MongoMapper::Document

  key :name, String
end

user = User.create( :name => "Hello" )
user.name = "Hello?"

puts user.name # => Hello?

Is there an easier way to do this? I know that in DataMapper, I can edit multiple keys (or properties, in DM's case) at once, but with MM, I can only do one at a time.

Am I missing something, or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

人│生佛魔见 2024-11-21 07:28:28

您可以像编辑 ActiveRecord 对象一样编辑文档/对象:为属性分配一些值,然后调用save

您的示例只有一个键,因此这是一个具有多个键的键:

class User
    include MongoMapper::Document
    key :name, String
    key :email, String
    key :birthday, Date
    timestamps! # The usual ActiveRecord style timestamps
end

然后:

user = User.create(
    :name     => 'Bob',
    :email    => '[email protected]',
    :birthday => Date.today
)
user.save

然后:

user.name     = 'J.R.'
user.email    = '[email protected]'
user.birthday = Date.parse('1954-06-02')
user.save

或者有 update_attributes

user.update_attributes(
    :name  => 'J.R. "Bob" Dobbs',
    :email => '[email protected]'
)
user.save

也许我不确定您在问什么。

You edit your documents/objects the same way you'd edit an ActiveRecord object: assign some values to attributes and then call save.

Your example only has one key so here's one with multiple keys:

class User
    include MongoMapper::Document
    key :name, String
    key :email, String
    key :birthday, Date
    timestamps! # The usual ActiveRecord style timestamps
end

And then:

user = User.create(
    :name     => 'Bob',
    :email    => '[email protected]',
    :birthday => Date.today
)
user.save

And later:

user.name     = 'J.R.'
user.email    = '[email protected]'
user.birthday = Date.parse('1954-06-02')
user.save

Or there's update_attributes:

user.update_attributes(
    :name  => 'J.R. "Bob" Dobbs',
    :email => '[email protected]'
)
user.save

Maybe I'm not sure what you're asking.

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