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.
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.
As in the example
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
Finally, add acts_as_paranoid to the target model.
class Event < ApplicationRecord acts_as_paranoid end
** Installation completed! !! ** **
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.
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?
destroyThe recorded record is deleted like the image_The time of logical deletion remains in the column called at.
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.
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 ^ ^