我有以下(经过消毒的)模型:
class Person < ActiveRecord::Base
attr_accessible :name, :first_name, :last_name, :age, :job_title, :salary, :ssn, :prison_convictions, :addresses_attributes
has_many :addresses, inverse_of: :person
accepts_nested_attributes_for :addresses, allow_destroy: true
end
class Address < ActiveRecord::Base
attr_accessible :zip_code, :street,:house_number,
:unique_per_person_government_id
belongs_to :person, inverse_of: :addresses
validates_uniqueness_of :unique_per_person_government_id, scope: :person_id
end
问题如下,
假设人 Joe Shmoe 目前有两个地址附加到他自己
666 Foo Street, 12345,唯一 ID:“ABCDEFG”和 777 Lucky Avenue,54321,唯一 ID:“GFEDCBA”</p>
假设以下帖子来自一个表单:
{:addresses_attributes =>
{ [0] => {:unique_per_person_government_id => “ABCDEFG”, :street=> “Foo Street”, :house_number => 666, :zip_code=>12345, _destroy => 1}
[1] => {:unique_per_person_government_id => “ABCDEFG”, :street=>”Bar Street”, :house_number => 888, :zip_code => 12345, _destroy => 0}
}
行为似乎是第二个(即新的)记录首先验证唯一性,验证失败。
我想要的行为是marked_for_destruction?
在进行任何进一步验证之前非常简单地删除所有元素。
如何以这种方式重新连接生命周期?有没有更好的方法来实现这一目标?
谢谢!