值大于 0.1

Value greater than 0.1

我正在研究 rails 3.2 的旧版本。该列是 string,我需要验证该值是 <= 0.1。来自 documentation 状态 "By default, it will match an optional sign followed by an integral or floating point number".

validates :min_amount, numericality: { greater_than_or_equal_to: 0.1 }

但即使我提交 0.5 它说它不大于或等于 0.1

使用 to_f 将字符串转换为浮点小数。或者使该列本身成为浮点数。

试试这个,它应该会自动将值转换为浮点数:

validates :min_amount, numericality: { greater_than_or_equal_to: 0.1, only_float: true }