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