什么是不允许整数列的负数的方法?
我想也许这会奏效,但事实并非如此:
Category_Attribute_Identifier
编辑:
在上下文中,有一个夜间的cron执行一堆计算,并使用validates_numericality_of :rolling_rank, :only_integer => true, :greater_than_or_equal_to => 0
。如果递减试图进入否定,我希望它停止(并保存)为零。
答案 0 :(得分:1)
From the docs for decrement!
(emphasis mine):
decrement!(attribute, by = 1)
Wrapper around
decrement
that saves the record. This method differs from its non-bang version in that it passes through the attribute setter. Saving is not subjected to validation checks. Returnstrue
if the record could be saved.
If this is what you want...
If the decrement tries to go into the negative, I want it to stop (and save) at zero.
...then you don't want a validation.
I think the simplest solution is to override the rolling_rank=
setter:
def rolling_rank=(value)
value = 0 if value < 0
super
end
Since decrement!
"passes [the value] through the attribute setter," this will be invoked before the record is persisted.