In my project, discount has and belongs to many businesses. I have an association callback rule that a discount can't remove its last businss (to maintain that there is always a businss for each discount):
has_and_belongs_to_many :businesses,
before_remove: :check_count,
def check_count
raise 'Cannot remove latest business!' if businesses.count == 1
end
However I realized that I won't be able to remove discounts. Since when I do discount.destroy
, I think it will try to remove businesses, and in term will raise the error.
So is there a way around this, like some kind of :except
as in controllers?
You can call discount.delete
which will skip the callbacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With