這是我期望的一個非常簡單的問題,但我無法在指南或其他地方找到明確的答案。Rails 3 Validation:presence => false
我在ActiveRecord上有兩個屬性。我想要一個在場,另一個是零或空白字符串。
我該怎麼做相當於:presence => false?我想確保價值爲零。
validates :first_attribute, :presence => true, :if => "second_attribute.blank?"
validates :second_attribute, :presence => true, :if => "first_attribute.blank?"
# The two lines below fail because 'false' is an invalid option
validates :first_attribute, :presence => false, :if => "!second_attribute.blank?"
validates :second_attribute, :presence => false, :if => "!first_attribute.blank?"
或者,也許有一個更優雅的方式來做到這一點...
我運行的Rails 3.0.9
我不知道你所需要的:存在=>假都在代碼的最後兩行。 – creativetechnologist 2012-04-09 09:36:48
@creativetechnologist它需要某種測試。如果我擺脫:存在驗證,它給了我:C:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3。在驗證中:你需要提供至少一個驗證(ArgumentError) – LikeMaBell 2012-04-10 07:10:08
值得注意Rails 4這叫做validates_absence_of。 – mpowered 2014-12-11 00:52:13