Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def protect_master_user
- unless User.find_all_by_account_id_and_is_master(self.account_id, true).count > 1
- false
- end
- end
- class Account < ActiveRecord::Base
- has_many :users
- before_destroy :destroy_users
- protected
- def destroy_users
- users.each do |user|
- u.account_id = nil
- u.destroy
- end
- end
- end
- class User < ActiveRecord::Base
- belongs_to :account
- before_destroy :protect_master_user
- protected
- def protect_master_user
- unless account_id.nil? ||! master ||
- User.find_all_by_account_id_and_master(self.account_id, true).count > 1
- errors.add_to_base "Cannot remove master user."
- return false
- end
- end
- end
Add Comment
Please, Sign In to add comment