这是我期望的一个非常简单的问题,但我无法在指南或其他地方找到明确的答案。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