2014-02-11 28 views
1

我看,這mongoid具有復位提起方法:mongoid:復位模式如何改變

person = Person.first 
person.name = "Alan Garner" 

# Reset the changed name back to the original 
person.reset_name! 

但我在我的模型許多領域,並且在任何時候,一些IT領域可以成爲無效。 1.如何重置模型中的無效字段? 2.如何將所有模型重置爲初始狀態?

回答

0

,你可以簡單地嘗試:

person = Person.first 

#for invalid 
person.errors.each do |field, message| 
    method_name = "reset_#{field}!" 
    person.send(method_name) 
end 

#for all 
person.attributes.each do |field, value| 
    method_name = "reset_#{field}!" 
    person.send(method_name) 
end 
0

你也可以做到這一點,可以將所有字段:

person.reload 

所以,很簡單,但點擊數據庫。