比方說,我們有一個模型中的方法是模型方法本身應該調用「保存」嗎?
- 需要在保存記錄只叫
- 可以更新模型本身,因此該模型需要重新保存後記
如果「保存」呼叫發生在方法本身內部,如下面的代碼
def result
save! if new_record?
# do some funky stuff here that may also change the model state
# ...
# And calculate the return value
search_result = "foo" # Let's say "foo" is the value we calculated
save! if changed?
search_result # return
end
或者應該外部觀察者(th e控制器)負責根據需要調用保存?
以某種方式相關的問題:http://stackoverflow.com/questions/3125198/should-a-modifying-class-method-save-itself-or-be-explicity-called-after-the-meth – 2010-10-09 16:36:52
此演示文稿測試可能能夠幫助您找出該模型上的界面變得奇怪的地方。 https://github.com/jimweirich/presentation_testing_why_dont_we_do_it_like_this/raw/master/pdf/testing.key.pdf如果您覺得在測試應用程序時必須打到分貝,那麼可能有一些功能可以考慮到這將使架構更易於使用。從第pg開始。 69. – 2011-02-07 21:15:21
不知道它是如何與問題實際相關的,但是出色的表現 - 感謝分享! – 2011-02-09 06:08:18