Skip to content

Instantly share code, notes, and snippets.

@theareba
Last active August 14, 2016 17:11
Show Gist options
  • Save theareba/b87d1b5d7bbd8ea52dafaa7cf82a1b00 to your computer and use it in GitHub Desktop.
Save theareba/b87d1b5d7bbd8ea52dafaa7cf82a1b00 to your computer and use it in GitHub Desktop.
class Comment < ApplicationRecord
include Trashable
end
class Post < ApplicationRecord
include Trashable
end
Comment.first(5).map(&:trash!)
Post.first(5).map(&:trash!)
module Trashable
extend ActiveSupport::Concern
included do
default_scope -> { where(trashed_at: nil) }
scope :trashed, -> { unscoped.where("#{self.table_name}.trashed_at is not null") }
end
def trash!
update_attribute :trashed_at, Time.now
end
def trashed?
trashed_at.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment