2011-06-05 76 views
2

MongoMapper文档中没有任何地方可以找到任何实际编辑文档的方法。我在其他地方也找不到任何东西。我能找到的唯一方法,就是这个方法:编辑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,我一次只能做一个。

我错过了什么,或者是什么?

回答

4

您可以像编辑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 

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

+0

我正在寻找'user.update_attributes',谢谢。 – 2011-06-05 13:13:55