属于Rails 5的存在无法正常工作 [英] Belongs_to presence in Rails 5 not working

查看:69
本文介绍了属于Rails 5的存在无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Rails 5中的新默认值要求存在 belongs_to 关联。我使用这种关联建立了一个模型,但是问题是当关联的字段为空时,我没有收到状态验证错误。相反,由于我将 _id 列设置为不为空,所以我收到了数据库Null Validation错误。 (PG :: NotNullViolation,因为我使用Postgres)

To my knowledge, the new default in Rails 5 requires belongs_to associations to be present. I made a model with this association, but the problem is I don't get presence validation error when the associated field is empty. Instead I get a database Null Validation error since I set the _id column not to be null. (PG::NotNullViolation because I use Postgres)

此行为正常吗?我的意思是我不应该只得到rails错误吗?

Is this behaviour normal? I mean shouldn't I get the rails error only?

BTW,当我为该字段添加在线状态验证时,它会按预期工作。

BTW, when I add presence validation for the field, it works as I expected.

推荐答案

根据问题 config的怪异行为belongs_to_required_by_default ,似乎您的其他宝石之一干预了 ActiveRecord :: Base 并导致了错误。

According to the issue re weird behaviour of config belongs_to_required_by_default, it seems like one of your other gems intervenes in ActiveRecord::Base and causes the bug.

该问题的一种解决方法是移动线路

One of workarounds to the issue is to move the line

config.active_record.belongs_to_required_by_default = true

从初始化程序直接放入 application.rb

from initializers directly into application.rb.

这对我来说很顺利。

这篇关于属于Rails 5的存在无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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