Last active
December 21, 2015 17:28
-
-
Save vlado/6340556 to your computer and use it in GitHub Desktop.
Soft Delete for Rails (ActiveRecord)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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