If you have a method called "validate" on your model, this is used
automatically by the valid? method. No need to do anything else.
The only problem with that is if you have subclasses that override the
validate method. Unless the subclass calls the superclass method,
your validations won't work as expected.
class Order < AR::Base
def validate
# this will never be called
end
end
class RushOrder < Order
def validate
self.errors.add(:status, 'invalid') if self.status.blank?
end
end
If you do this instead:
class Order < AR::Base
before_validation :check_base_order
def check_base_order
# this will always be called
end
end
class RushOrder < Order
before_validation :check_rush_order
def check_rush_order
# this will ALSO always be called
end
end
Yes, I meant model, but wrote controller. Also it should be
@tweedledum etc. The 'unless' is a continuation of the 'errors.add'
line, but it's come out with bad formatting.