I have a User class with a field called admin which is a boolean that
determines if the user is or is not an administrator. I want to make it
impossible for the last administrator for an account to be removed from
I need to protect against this both when deleting a user and when
editing a user as you can revoke a user’s administrator privileges via a
User belongs_to :account.
Account has_many :Users
I was thinking that the best way to do this would involve either
before_update and before_destroy or a validation. I think I know how to
write the before_delete as that just involves knowing which user you are
going to delete and checking if its account has more than one
administrator. I am unsure how to write before_update as that requires
not only knowing which user you are going to update but also if the
updated info would make that user no longer have admin status. I have
no idea where to start for the validation if I was to go that route for
similar reasons as the before_update.
Do any of you have any examples of code that prevents the
deletion/update of an object based on a particular field’s value?