我有发货和发票。
发票属于装运
货件有一张发票
如果货件有发票,则不能删除货件。我需要在模型中设置它,因为我使用的是ActiveAdmin。
所以我在shipment.rb中做了这个
has_one :invoice
before_destroy :check_for_invoice
private
def check_for_invoice
unless invoice.nil?
self.errors[:base] << "Cannot delete shipment while its invoice exists."
end
end但我刚收到一条黄色消息,说“货件不能被删除”,但它实际上已经被删除了。
如何防止货件被删除?
发布于 2012-01-12 03:34:21
before_destroy回调需要一个true/false值来确定是否继续。
将return false添加到您的check_for_invoice,如下所示:
has_one :invoice
before_destroy :check_for_invoice
private
def check_for_invoice
unless invoice.nil?
self.errors[:base] << "Cannot delete shipment while its invoice exists."
return false
end
end 发布于 2013-02-12 20:27:23
我在shipment.rb中的2分钱
has_one :invoice, dependent: :restrict我认为它会起作用,我在另一个线程中看到了这个解决方案。我现在正在尝试我的模型。
发布于 2012-01-12 03:28:32
从docs
如果before_*回调返回false,则取消所有以后的回调和关联的操作。
所以试试这个:
self.errors[:base] << "Cannot delete shipment while its invoice exists." and return falsehttps://stackoverflow.com/questions/8825354
复制相似问题