在 Rails 中,除了验证错误之外,如何找出导致 .save() 失败的原因? [英] In rails, how can I find out what caused a .save() to fail, other than validation errors?

查看:22
本文介绍了在 Rails 中,除了验证错误之外,如何找出导致 .save() 失败的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ActiveRecord 模型,它从 valid? 返回 true(并且 .errors 为空),但是从 false 返回false代码>保存().如果模型实例有效,我如何找出导致保存失败的原因?

I have an ActiveRecord model which is returning true from valid? (and .errors is empty), but is returning false from save(). If the model instance is valid, how can I find out what's causing the save to fail?

推荐答案

检查所有回调.

我遇到了这样的问题,我有一个after_validate"方法在我对模型进行了一系列更改后失败了.该模型是有效的,但after_validate"返回 false,所以如果我使用 model.valid 它说是真的,但是如果我保存它会给我验证错误(从 after_validate 回调传递).很奇怪.

I had a problem like this where I had and "after_validate" method that was failing after I had made a bunch of changes to the model. The model was valid but the "after_validate" was returning false, so if I used model.valid it said true, but then if I saved it gave me validation errors (passed through from the after_validate callback). It was weird.

查看应用程序跟踪,您应该能够看到哪行代码引发了异常.

Look at the application trace and you should be able to see what line of code is raising the exception.

这篇关于在 Rails 中,除了验证错误之外,如何找出导致 .save() 失败的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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