Rails 2.3.8:在保存和验证之前发生的回调(即使没有验证)
我使用 before_validation 在验证模型之前对模型进行一些更新,然后保存。
显然,当您在未经验证的情况下保存时,不会调用验证回调。
我应该使用什么回调来确保此代码在保存之前运行,并且在验证之前运行(如果存在)?
I was using before_validation, to do a few updates to the models before they are being validated and later on - saved.
Apparently, the validation callbacks aren't called when you save without validation.
What callback should I use to make sure this code runs before the save and before validation if it exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
跳过验证听起来像是一种解决方法。如果在某些情况下您不想验证,最好有条件地调用验证。这样它也是可测试的。
这看起来很hacky但是......
Skipping validation sounds like a workaround. You're better off calling validations conditionally if there are cases when you don't want to validate. That way it's testable as well.
This seems pretty hacky but...