2013-04-02 31 views
-2

我使用的鐵軌2.3.8版本是有原因update_attributes方法不會觸發before_validation回調什麼特別的原因爲什麼update_attributes方法並沒有叫before_validation再打在我的模型

class x < ActiveRecord::Base 
before_validation :testing 

def testing 
    debugger 
end 
end 

x.update_attributes(:名稱=> 「你好」)不會觸發before_validation

+0

你的標題說'update_attributes',你的問題說'update_attribute'和'update_attributes',你的意思是?他們是截然不同的。 –

+0

它只是x.update_attributes update_attribute的格式爲update_attribute(name,value)它是一個你應該通過參數格式理解的錯字錯誤 – suman

+0

你的'testing'方法沒有運行,有什麼跡象?在你的例子中,該方法沒有body和IIRC,只有從回調中返回false纔會停止執行鏈,而不是nil。 –

回答

6

如果您使用了此方法:here is the documentation,該答案將非常明確,其中明確指出update_attribute未通過驗證過程。如果你想觸發驗證,你應該使用update_attributes

+0

我的驗證不適用於update_attributes .. – suman

+0

原始問題提到'update_attributes',而不是'update_attribute'。 – BalinKingOfMoria

相關問題