Skip to content

Instantly share code, notes, and snippets.

@EdwardTippett
Last active December 10, 2015 20:48
Show Gist options
  • Save EdwardTippett/4490840 to your computer and use it in GitHub Desktop.
Save EdwardTippett/4490840 to your computer and use it in GitHub Desktop.
def set_deleted_at(value)
return self unless is_permanent?
record = self.class
record = record.unscoped if active_record_3?
record = record.find(id)
record.deleted_at = value
begin
# we call save! instead of update_attribute so an ActiveRecord::RecordInvalid
# error will be raised if the record isn't valid. (This prevents reviving records that
# disregard validation constraints,)
# run validations when trying to revive a record
value == nil ? record.save! : record.save!(:validate => false)
@attributes, @attributes_cache = record.attributes, record.attributes
rescue Exception => e
# trigger dependent record destruction (they were revived before this record,
# which cannot be revived due to validations)
record.destroy
raise e
end
end
@lengarvey
Copy link

Good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment