Skip to content

Instantly share code, notes, and snippets.

@vlado
Last active December 21, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlado/6340556 to your computer and use it in GitHub Desktop.
Save vlado/6340556 to your computer and use it in GitHub Desktop.
Soft Delete for Rails (ActiveRecord)
module SoftDelete
def self.included(within)
within.class_eval do
scope :deleted, where("#{quoted_table_name}.deleted_at IS NOT NULL")
scope :not_deleted, where("#{quoted_table_name}.deleted_at IS NULL")
end
end
def not_deleted!
raise ActiveRecord::RecordNotFound if deleted?
self
end
def deleted?
deleted_at?
end
def soft_delete!
update_attribute(:deleted_at, Time.now)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment