我相信這應該這樣做:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:if => :invalid?
def invalid?
state == 'invalid'
end
你也可以內聯到:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:if => lambda{ state == 'invalid' }
希望這有助於。
如果你要做到這一點,當狀態not
無效的,那麼你可以做的是兩種方式:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:unless => :invalid?
或者,你可以改變它了一點,有一個有效的消息,這可能是我喜歡:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:if => :valid?
def valid?
state != 'invalid'
end
好吧,所以我很困惑,如果我需要驗證才能生效,只有當以前的應用程序「不」有一個狀態爲無效......所以它= =! – Trace 2012-03-27 02:09:58
對,因爲當它不是無效的,那麼你只會說狀態!='無效' – 2012-03-27 21:14:38