子供がいる場合にレコードが破棄されないようにしています。
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 により、Quotation レコードが削除されなくなります。
ただし、Submission before_destroy コールバックの :check_for_payments は、Submission の削除を停止しません。
支払いのある提出物が破棄されないようにするにはどうすればよいですか?