-2

我有两个字段:

验证:bhp

验证:ps

现在如果 :bhp 的值为 80,

那么 :ps 的值应该在 :bhp 的 +-10 范围内,例如。[70..90] 在这里。

反之亦然

4

1 回答 1

0

You can use custom validator.

Something like this:

validate :keep_ps_in_range_of_bhp

def keep_ps_in_range_of_bhp
  range = 10
  unless (bhp-ps).abs <= range
    errors.add(:ps, "should be in range +-#{range} of bhp")
  end
end

Similar way you can add validator for the opposite case, but it makes sense only if you don't allow to change both this values (but even in this case I don't sure that it is really good approach).

于 2012-05-11T09:51:23.747 回答