我想确定的是,在我的Rails4应用程序中,通过验证,两个属性的值并不相同。我知道确认验证,但我需要的正好相反。
Rails有这种验证吗?
发布于 2014-04-26 09:55:49
您需要创建一个自定义验证,我认为:
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希望它有帮助:)
发布于 2016-08-25 14:19:39
我也有类似的需求,想要一个简单的解决方案。我觉得这最终效果很好。
validates :applicant_id, exclusion: { in: -> (reference_request) { [reference_request.reference_id] }, message: 'cannot also be a reference' }
https://stackoverflow.com/questions/23309026
复制相似问题