我有一个模特儿购买:
class Purchase < ActiveRecord::Base
has_many :purchase_items, dependent: :destroy
accepts_nested_attributes_for :purchase_items, reject_if: :all_blank, allow_destroy: true
validates_length_of :purchase_items, minimum: 1
end和PurchaseItem,包括:
class PurchaseItem < ActiveRecord::Base
belongs_to :purchase
end假设我只买了一件东西。如果我通过以下方式将该物品标记为销毁:
purchase.purchase_items.first.mark_for_destruction
purchase.save!采购保存良好,使其在DB中没有任何引用项。
检查ActiveModel::Validations::LengthValidator中的validate_each方法,我们可以看到它没有验证正在验证的值是否有被标记为销毁的对象。
这是正常的行为,还是实际上是一个问题?如果它是正常的,那么验证marked_for_destruction对象的关系长度的正确方法是什么?
(当然,没有自定义验证器.)
发布于 2013-09-09 21:07:56
https://stackoverflow.com/questions/18706860
复制相似问题