Rails STI 验证继承
我的 Rails 应用程序中有 STI 模型。祖先模型使用 validates_... 方法进行验证,该方法运行良好。
但我也有自定义验证,并且我想在后代中添加更多不同的自定义验证。这些自定义验证取决于类。
如果我写
class DescendantA < Ancestor
protected
def validate
# ...
end
end
它只是覆盖原始验证,所以我失去了原始继承的验证。
Rails 中有这样做的约定吗?
I have STI models in my Rails application. The ancestor model has validations with the validates_...
methods which are working fine.
But I have custom validations as well, and I would like to add more different custom validations in the descendants. These custom validations would depend on the class.
If I write
class DescendantA < Ancestor
protected
def validate
# ...
end
end
It simply overwrites the original validations, so I loose the original inherited validations.
Is there a convention to do this in Rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在子级的
validate
方法末尾添加super
还不够吗,这样父级的validate
方法也会被调用?Would it not be sufficient to add
super
at the end of thevalidate
method in the child, so that the parent'svalidate
method would also be called?