问题
Should I extend or include ActiveModel:Validations:Callbacks:ClassMethods
or ActiveModel:Validations:Callbacks
?
回答1:
I got it to work like this:
class Foo
extend ActiveModel::Callbacks
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
before_validation :bar
def bar
# callback logic here
end
end
It's important that you have everything in that order.
来源:https://stackoverflow.com/questions/8936906/whats-the-correct-way-to-make-before-validation-etc-work-in-an-activemodel