我有所需的屬性的標準的活動記錄模式:如何在驗證運行之後但保存之前將模型實例設置爲無效?
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方法需要大小屬性,但執行的是需要被驗證以及結果。在以上的情況下,在其存在被驗證之前使用大小。
我可以將實例設置爲無效並在所有驗證完成後停止保存過程?