- Restoring the record
- Documenting the record in pre deletion points in time
- Statistics
rails g migration add_active_to_people active:booleanand running rails db:migrate. The model:
class Person < ActiveRecord::Base attr_accessible :name validates :active, :inclusion => { :in => [true, false] } before_validation :activate, :on => :create, :if => Proc.new {|r| r.active.nil? } def self.active where :active => true end def activate self.active = true self end def deactivate self.active = false self end endThe model expects the boolean active flag to be set with nothing else than true or false.
Furthermore every new person record will be initialized with true in a callback before validating it. That means if nothing else was set, the instance method activate is called, which does exactly set the flag to true by default.
A named scope appends a selection to the finder chain, returning all active people. A usage example:
Person.activeIf you are not familiar with ActiveRecord scopes read 'Scope the model!'.
The second instance method deactivate deactivates the record. In fact it prepares soft deletion by setting the 'active' flag to false like:
Person.find(1).deactivateand touching he database:
Person.find(1).deactivate.savewhich will generate the SQL on a MariaDB system:
UPDATE 'people' SET 'active' = 0, WHERE 'people'.'id' = 1Well that could be wrapped behind:
def deactivate! deactivate.save! endAnother reasonable data type for soft deletion columns is datetime like deleted_at. I would suggest using it, when the timestamp of the deletion has to be documented as well.
Supported by Ruby 2.1.1 and Ruby on Rails 3.2.17
Keine Kommentare:
Kommentar veröffentlichen