I have shipments and invoices.

invoice belongs to shipment
shipment has one invoice

If the shipment does have an invoice, then the shipment shouldn't be able to be deleted. I need to set this up in the models because I'm using ActiveAdmin.

So I did this in 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

But I just get a yellow message saying "Shipment cannot be deleted" but it was in fact deleted.

How can I prevent the shipment from being deleted?

Edit
Report