2

我有以下型号:

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打电话)

4

0 回答 0