2010-02-27 87 views

回答

7
class Model < ActiveRecord::Base 

    validates_presence_of :address, :if => :city? 

end 

:address:cityModel兩個屬性。

+0

這一工程!謝謝。我最終需要指定一個proc。查看我的回答,瞭解'if'屬性接受的三種不同的事情。 – 2010-02-27 07:24:18

5

validates_presence_of接受if屬性,該屬性根據documentation:字符串,方法或過程採取三件事之一。

if - Specifies a method, proc or string to call to determine if the validation 
should occur (e.g. :if => :allow_validation, or 
:if => Proc.new { |user| user.signup_step > 2 }). 
The method, proc or string should return or evaluate to a true or false value. 

我最終需要使用一個進程,因爲我想確保某個參數被填寫驗證之前:

validates_presence_of :bar, :if => Proc.new { |foo| !foo.age.blank? } 
+0

而不是創建和回答,我建議編輯您的原始問題。 – 2010-02-27 10:48:14

+0

謝謝!這就是我一直在尋找的! – JGutierrezC 2015-12-09 03:00:00

相關問題