[Rails] Implement event end function (logical deletion) using paranoia (gem)

What is logical deletion in the first place?

Rails ActiveRecord delete is usually a physical delete, which actually deletes the data from the database. On the other hand, logical deletion does not actually delete the data, but sets a column called a flag that is considered to be deleted, and behaves as if it is deleted by the user, and can be restored to the original state when necessary. is. I think that logical deletion can be used in cases such as "The user's data must be retained for a certain period of time on the system even after the user withdraws" or "I want to temporarily freeze the account due to a violation etc." I will. Rails can be easily implemented by using a gem library called paranoia. This gem is a reimplementation of the acts_as_paranoid gem.

Quote (https://remonote.jp/rails-gem-paranoia)

There was an easy-to-understand article, so I quoted it. Normally, as mentioned above, it seems to be a function used when the management user has a ** violation etc. and wants to temporarily freeze the account **, but in my case ** event deletion function and termination function Implemented logical delete to separate. ** **

What does that mean? It would be nice if you could see the events that the user has attended in the past while implementing the event attendance application. **have become. Naturally, when the event is over, the event data will be deleted so that it can not be seen in the event list, but what I wanted to implement is ** It is not displayed in the list, but you can see the event you participated in on the user's My Page something like. In other words, although it is superficially deleted, the record information remains. ** But I didn't know how to implement it.

At first, when I deleted an event, I was looking for a way to move the entire information of that event to a different table, but when I couldn't find the information, I could easily implement logical deletion ** paranoia **. I found a gem.

Although the introduction has become long, I would like to summarize the flow from introduction to implementation immediately.

step1: Introduction

gem 'paranoia'

As in the example bundle install

Then add a column.

rails g migration AddDeletedAtToEvents deleted_at:datetime:index

Add a column called ** deleted_at ** to the event model.

class AddDeletedAtToEvents < ActiveRecord::Migration[6.0]
  def change
    add_column :studies, :deleted_at, :datetime
    add_index :studies, :deleted_at
  end
end
rails db:migrate

Finally, add acts_as_paranoid to the target model.

Event.rb


class Event < ApplicationRecord
  acts_as_paranoid
end

** Installation completed! !! ** **

step2: Set the routing

routes.rb


Rails.application.routes.draw do
  root "events#index"
  resources :events do
    member do
      delete 'finish'
    end
  end
end

Since there is no action to end among the seven actions generated by resources, set the routing of the finish action by nesting in the event.

step: 3 Define the finish action on the controller

events_controller.rb


 def destroy
   @event = current_user.events.find(params[:id])
   @event.really_destroy!
   redirect_to root_path, notice: "Deleted the event"
 end

 def finish
   @event = current_user.events.find(params[:id])
   @event.destroy!
   redirect_to root_path, notice: "The event has ended"
  end

The difference between the destroy action and the finish action above is that after `@ event```, if you really want to delete it from the record, write` really_destroy. Then, in the finish action, it is usually described as `` `destroy, but how does it actually remain in the database?

746eace97e67b286975d2e087964212e.png destroyThe recorded record is deleted like the image_The time of logical deletion remains in the column called at.

step: 4 Get the logically deleted data with the find method

users_controller.rb


  def show
     @user=User.find(params[:id])
     @events = @user.events.only_deleted
  end

Since we want to display past events on the user's detail page, we can fetch only the logically deleted records by writing only_deleted in the user controller.

Finally,,

It seems that paranoia can use various other methods, so please refer to the official document ^ ^ Official link (https://github.com/rubysherpas/paranoia)

Thank you for watching till the end ^ ^

Recommended Posts

[Rails] Implement event end function (logical deletion) using paranoia (gem)
Logical deletion using Gem paranoia
[Ruby on Rails] Logical deletion (withdrawal function)
Implement star rating function using Raty in Rails6
[Rails] Implement search function
Rails learning How to implement search function using ActiveModel
Try to implement tagging function using rails and js
Implement application function in Rails
[Rails] Implement User search function
Search function using [rails] ransack
Implement follow function in Rails
Implement category function using ancestory
Ajax bookmark function using Rails
[Rails] Implement image posting function
[Rails] I tried to implement "Like function" using rails and js
[Rails] Implement community membership application / approval function using many-to-many associations
Implement a refined search function for multiple models without Rails5 gem.
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Tag management function (using acts-as-taggable-on)
Implement simple login function in Rails
[Rails] Comment function (registration / display / deletion)
[Rails] gem ancestry category function implementation
Implement CSV download function in Rails
[Rails] Manage multiple models using devise gem
[Rails] Create an evaluation function using raty.js
How to implement image posting using rails
Implement button transitions using link_to in Rails
Multiple image upload function using Rails Carrierwave
How to implement image posting function using Active Storage in Ruby on Rails