如果有孩子,我试图防止记录被破坏。
class Submission < ActiveRecord::Base
has_many :quotations, :dependent => :destroy
before_destroy :check_for_payments
def quoted?
quotations.any?
end
def has_payments?
true if quotations.detect {|q| q.payment}
end
private
def check_for_payments
if quoted? && has_payments?
errors[:base] << "cannot delete submission that has already been paid"
false
end
end
end
class Quotation < ActiveRecord::Base
#associations
belongs_to :submission
has_one :payment_notification
has_one :payment
before_destroy :check_for_payments
private
def check_for_payments
if payment_notification || payment
errors[:base] << "cannot delete quotation while payment exist"
return false
end
end
end
当我测试此代码时, before_destroy :check_for_payments 会阻止删除报价记录。
但是,提交 before_destroy 回调中的 :check_for_payments 不会阻止提交被删除。
我怎样才能阻止提交的付款被销毁?