2013-01-19 22 views
2

我有一個mongoid文檔(在rails應用程序中)驗證失敗。我想重置所有無效字段。目前我正在這樣做:如何重置mongoid文檔的所有無效字段?

@product.errors.each do |e,m| 
    method_name = "reset_#{e}!" 
    @product.send(method_name) 
end 

這是好的,但沒有一個更好的(更簡潔)的方式來做到這一點?我已閱讀dirty tracking documentation並進行了Google搜索,但我無法找到任何關於此的信息。

回答

0

我不知道你是什麼意思與「更加簡潔」,但你並不需要使用「發送」

@product.errors.errors.each do |f| 
    @product[f] = c.changes[f.to_s].first 
end 
相關問題