Before_validation an before_validation_on_create

I’m not sure what’s the difference between before_validation an
before_validation_on_create. Is it that before_validation gets invoked
before a create and update call, whereas the latter just gets invoked
on create call?

Yes, exactly. before_validation happens before every validation. That
means:

model.valid?
model.save
model.create

before_validation_on_create only happens when saving a new record.

Andrew V. wrote in post #1071395:

Yes, exactly. before_validation happens before every validation. That
means:

model.valid?
model.save
model.create

before_validation_on_create only happens when saving a new record.

How do these before validation callbacks work? Are they just simple
function callbacks, or can you return false or errors.add in them to
halt the rest of the record.save code?

On 6 August 2012 14:30, masta Blasta [email protected] wrote:

How do these before validation callbacks work? Are they just simple
function callbacks, or can you return false or errors.add in them to
halt the rest of the record.save code?

Have a look at the Rails Guide on ActiveRecord Validations and
Callbacks. That may answer your questions.

Colin

Colin L. wrote in post #1071428:

On 6 August 2012 14:30, masta Blasta [email protected] wrote:

How do these before validation callbacks work? Are they just simple
function callbacks, or can you return false or errors.add in them to
halt the rest of the record.save code?

Have a look at the Rails Guide on ActiveRecord Validations and
Callbacks. That may answer your questions.

Colin

Thanks for the pointer.

Here’s the quick answer for future users, from RoR api:

"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 ActiveRecord::Validations#save! is called it will raise a
ActiveRecord::RecordInvalid exception. Nothing will be appended to the
errors object.

If a before_* callback returns false, all the later callbacks and the
associated action are cancelled. If an after_* callback returns false,
all the later callbacks are cancelled. Callbacks are generally run in
the order they are defined, with the exception of callbacks defined as
methods on the model, which are called last."