我有所需的属性的标准的活动记录模式:如何在验证运行之后但保存之前将模型实例设置为无效?
class Sample < ActiveRecord::Base
has_many :colors
before_validation :grab_colors
validates_presence_of :size
validate :number_of_colors
private
def grab_colors
# grab x number of colors | x = size
end
def number_of_colors
self.errors.add("size","is to large.") if colors.count < size
end
end
我的问题是,grab_colors方法需要大小属性,但执行的是需要被验证以及结果。在以上的情况下,在其存在被验证之前使用大小。
我可以将实例设置为无效并在所有验证完成后停止保存过程?