I have three classes: School , Account and Administratorship .
School
has_many :administatorships has_many :administrators, :through => :administratorships
Account
has_many :administratorships
Administratorship
belongs_to :account belongs_to :school before_destroy :confirm_presence_of_alternate_administratorship_in_school protected def confirm_presence_of_alternate_administratorship_in_school unless school.administrators.count(["administratorships.account_id != #{id}"]) > 0 errors.add_to_base "The school must have at least one administrator" end end
Now I would like for me to call the destroy command on the Administratorship instance so that it adds an error to the model and prevents the model from breaking. I removed the unless to see if this prevents the error from being added, but it isnโt. It seems that the presence of errors in the model does not prevent destruction.
So my question is, is there a way to prevent destruction with checks? I understand that I could define a method that destroys only if the above condition is met, but it seems that the validation approach is a more elegant solution.
validation ruby-on-rails
tanman Apr 02 2018-11-11T00: 00Z
source share