Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Created January 22, 2022 14:31
Show Gist options
  • Save stevepolitodesign/49f4e71f859266df83ba0c76137427a9 to your computer and use it in GitHub Desktop.
Save stevepolitodesign/49f4e71f859266df83ba0c76137427a9 to your computer and use it in GitHub Desktop.
Prevent the destruction of a record in Active Record
class Webpage < ApplicationRecord

  def self.delete_all
    raise ActiveRecord::DeleteRestrictionError
  end

  def delete
    destroy
  end

  def destroy
    raise ActiveRecord::DeleteRestrictionError
  end
end
Webpage.destroy_all
# => `destroy': Delete restriction error. (ActiveRecord::DeleteRestrictionError)
Webpage.delete_all
# => `delete_all': Delete restriction error. (ActiveRecord::DeleteRestrictionError)
Webpage.first.destroy
# => `destroy': Delete restriction error. (ActiveRecord::DeleteRestrictionError)
Webpage.first.destroy!
# => `destroy': Delete restriction error. (ActiveRecord::DeleteRestrictionError)
Webpage.first.delete
# => `destroy': Delete restriction error. (ActiveRecord::DeleteRestrictionError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment