What's the correct way to make before_validation, etc. work in an ActiveModel

后端 未结 1 1717
青春惊慌失措
青春惊慌失措 2021-01-11 14:51

Should I extend or include ActiveModel:Validations:Callbacks:ClassMethods or ActiveModel:Validations:Callbacks?

1条回答
  •  执笔经年
    2021-01-11 15:18

    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.

    0 讨论(0)
提交回复
热议问题