使 before_validation 等在 ActiveModel 中工作的正确方法是什么 [英] What's the correct way to make before_validation, etc. work in an ActiveModel

查看:38
本文介绍了使 before_validation 等在 ActiveModel 中工作的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该扩展还是包含 ActiveModel:Validations:Callbacks:ClassMethodsActiveModel:Validations:Callbacks?

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

推荐答案

我让它像这样工作:

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.

这篇关于使 before_validation 等在 ActiveModel 中工作的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆