Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip association callback in destroy

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?

like image 750
lulalala Avatar asked Dec 26 '11 10:12

lulalala


1 Answers

You can call discount.delete which will skip the callbacks.

like image 175
Andrew Nesbitt Avatar answered Oct 23 '22 03:10

Andrew Nesbitt