在两个模型中使用 accepts_nested_attributes_for 时出现重复的验证错误我可以避免这种情况吗?

问题描述

我有以下型号:

class User < ApplicationRecord
  belongs_to :company

  accepts_nested_attributes_for :company
end
class Company < ApplicationRecord
  has_one :user

  accepts_nested_attributes_for :user,update_only: true

  validates :name,presence: true
end

当我执行以下步骤时:

company.name = nil
company.user
company.valid?
company.errors.full_messages

我收到重复的错误

["Name can't be blank","User company name can't be blank"]

有什么通用的方法可以避免这种行为吗?

(从其中一个模型中删除 accepts_nested_attributes_for 不是我的解决方案,我也需要 .user 调用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)