gpt4 book ai didi

ruby - 编辑 MongoMapper 文件

转载 作者:可可西里 更新时间:2023-11-01 10:01:33 24 4
gpt4 key购买 nike

在 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,我一次只能编辑一个。

我是不是遗漏了什么,还是什么?

最佳答案

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

你的例子只有一个键,所以这里有一个有多个键的例子:

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 => 'bob@example.com',
:birthday => Date.today
)
user.save

之后:

user.name     = 'J.R.'
user.email = 'dobbs@example.com'
user.birthday = Date.parse('1954-06-02')
user.save

或者有update_attributes:

user.update_attributes(
:name => 'J.R. "Bob" Dobbs',
:email => 'slack@example.com'
)
user.save

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

关于ruby - 编辑 MongoMapper 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com