Today I found these comments in active record's callbacks.rb file:
# If the returning value of a before_validation callback can be evaluated to false, the process will be aborted and Base#save will return false. # If Base#save! is called it will raise a RecordNotSave error. # Nothing will be appended to the errors object.
This caused me a lot of frustration earlier when I was trying to get a model with the following code to save:
before_validation :ensure_boolean_value_for_finalized
private def ensure_boolean_value_for_finalized self.finalized = false unless self.finalized end
I'm wondering why before_validation will abort the process of saving. My conception of before_validation was that its purpose was only to prepare the model object for validation, and that its purpose was *not* to do pre-validation validation.
It's not a huge deal - I'm just wondering what the logic behind before_validation's behavior is.