Artem Kalinchuk Artem Kalinchuk - 1 year ago 126
Ruby Question

Ruby on Rails - after_validation if valid?

Right now, from what I know,

will be called even if the model fails the validations. Is there a way to only call it if the model is valid? I tried adding
return false unless self.valid?
in the
method but that triggers validation again and it creates an infinite loop.

Answer Source

The failing validations add to the errors for the record, so you could just check:

return false unless self.errors.empty?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download