我发现ActiveRecord似乎不必要地验证子记录的情况.事先道歉,因为这非常复杂.
这涉及通过以前使用但未以任何方式改变的关联.它发生在3.2到最近的主人.我不确定这是一个导致意外行为或某种错误的设计决定.
我从实际代码中减少了一个测试用例,如下所示:
楷模:
class A < ActiveRecord::Base
belongs_to :b
has_many :cs, :through => :b
before_validation { puts "A" }
end
class B < ActiveRecord::Base
has_many :as
has_many :cs
before_validation { puts "B" }
end
class C < ActiveRecord::Base
belongs_to :b
before_validation { puts "C" }
end
Run Code Online (Sandbox Code Playgroud)
移民:
class AddABC < ActiveRecord::Migration
def change
create_table :as do |t|
t.references :b
end
create_table :bs do |t|
end
create_table :cs do |t|
t.references :b
end
end
end
Run Code Online (Sandbox Code Playgroud)
在空数据库上运行时,触发它的简化测试用例是:
b = B.create!
c …Run Code Online (Sandbox Code Playgroud)