paranoia gem ideas (and code) adapted for rails 4.
Rails 4 defines ActiveRecord::Base#destroy!
so Paranoid2
gem use force: true
arg to force destroy.
Add this line to your application's Gemfile:
gem 'paranoid2'
And then execute:
$ bundle
Add deleted_at: datetime
to your model.
Generate and run migrations.
rails g migration AddDeletedAtToClients deleted_at:datetime
class AddDeletedAtToClients < ActiveRecord::Migration
def change
add_column :clients, :deleted_at, :datetime
end
end
class Client < ActiveRecord::Base
paranoid
end
c = Client.find(params[:id])
# will set destroyed_at time
c.destroy
# will restore object and all it's associations
c.restore
# will restore only this object without it's associations
c.restore(associations: false)
# will destroy object for real
c.destroy(force: true)
# also useful scopes are available
Client.with_deleted
Client.only_deleted
class Listing < ActiveRecord::Base
has_attached_file :image,
# ...
preserve_files: true
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request