Forum: Ruby on Rails unexpected behavior of before_validation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B1664e3af08e4a995b5b37c5a523abeb?d=identicon&s=25 (Guest)
on 2007-04-05 15:21
(Received via mailing list)
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

  def ensure_boolean_value_for_finalized
    self.finalized = false unless self.finalized

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.

This topic is locked and can not be replied to.