Rails验证 - 确认验证的反向

时间:2014-04-26 09:30:31

标签: ruby-on-rails-4

我想确定,在我的Rails4应用程序中,两个属性与验证的值不同。我知道确认验证,但我需要完全相反。

Rails有这种验证吗?

2 个答案:

答案 0 :(得分:1)

我认为你需要创建一个自定义验证:

validate :check_attribute1_and_attribute2

def check_attribute1_and_attribute2
  if attribute_1 == attribute_2
    errors.add( :column_2, ' Value 2 cannot be similar to Value 1!')
  end
end

希望有所帮助:)

答案 1 :(得分:0)

我有类似的需求,想要一个简单的解决方案。我认为最终效果很好。

validates :applicant_id, exclusion: { in: -> (reference_request) { [reference_request.reference_id] }, message: 'cannot also be a reference' }