3
唯一性驗證我對車型之一下面的驗證規則,軌道上更新
validates :reciept_num, :presence => true,
:numericality => { :only_integer => true },
:uniqueness => true,
:on => :update,
:if => "!status_id.nil?"`
現在,當我更新使用update_attributes
方法的對象它給了我下面的錯誤
reciept_num: has already been taken
。
更新對象時,我沒有更改reciept_num屬性?那麼,爲什麼驗證在更新時失敗?
如果我沒有更新值,它必須是舊的,因此應該通過驗證。我錯過了什麼。
感謝您的回覆。讀完這個後,我想到我創建的種子數據有重複的條目。 – robert 2011-12-24 03:40:00
而且,即使沒有條件,':uniqueness => true'實際上也不能保證唯一性,所以在數據庫中需要一個唯一的約束。 – 2011-12-24 03:40:26
畝太矮了,你能詳細點嗎? – maletor 2012-09-07 07:20:53