| Module | Caboose::Acts::Paranoid |
| In: |
lib/acts_as_paranoid.rb
|
Overrides some basic methods for the current model so that calling destroy sets a ‘deleted_at’ field to the current timestamp. This assumes the table has a deleted_at date/time field. Most normal model operations will work, but there will be some oddities.
class Widget < ActiveRecord::Base
acts_as_paranoid
end
Widget.find(:all)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL
Widget.find(:first, :conditions => ['title = ?', 'test'], :order => 'title')
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test' ORDER BY title LIMIT 1
Widget.find_with_deleted(:all)
# SELECT * FROM widgets
Widget.find(:all, :with_deleted => true)
# SELECT * FROM widgets
Widget.count
# SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL
Widget.count ['title = ?', 'test']
# SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test'
Widget.count_with_deleted
# SELECT COUNT(*) FROM widgets
@widget.destroy
# UPDATE widgets SET deleted_at = '2005-09-17 17:46:36' WHERE id = 1
@widget.destroy!
# DELETE FROM widgets WHERE id = 1