在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中,我一次只能编辑一个键。
我是不是漏掉了什么?
发布于 2011-06-05 11:31:14
编辑文档/对象的方式与编辑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 => '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也许我不确定你在问什么。
https://stackoverflow.com/questions/6240738
复制相似问题