创建时是否正在运行关联的验证?

问题描述 投票:0回答:1

我们有三堂课

class a
  has_many :b
  has_many :c, through: b
end

class b
  belongs_to :a
  belongs_to :b
end

class c
  has_many :b
  has_many :a, through: :b
  validates_uniqueness_of :name
end

如果尝试创建类c的实例时,如果数据库中某些类name的2个对象以相同的a值存在于数据库中,我很惊讶地看到对类c的验证正在运行!为什么我根本不应该触摸类c行时运行验证?

ruby-on-rails ruby-on-rails-5
1个回答
0
投票

class B如何属于class B

[class B应该属于class Cclass Ahas_many :c, through: b

class C也要具有has_many :a, through: :b,反之亦然。

© www.soinside.com 2019 - 2024. All rights reserved.