Search code examples
ruby-on-railsruby-on-rails-5

Rails use destroy soft_destroy, how to update deleted_by


Excuse me, I am using soft_destroy to delete my data, and then I want to fix the deleted_by data when I try to delete it. So I used before_destroy in the model to do the processing. But it seems to have no effect. How can I do? Please tell me. thanks

Controller

def destroy
   @project.soft_destroy
   head :no_content
end

Model

before_destroy :deleted_by


private

def deleted_by
  self.deleted_by = 101
end

After these executions, deleted_by does not become 101.


Solution

  • You can do it in controller

     before_action :set_project, only: [:show, :edit, :destroy, :update]
     before_action :deleted_by, only: [:destroy]
    
    private
    def set_project
      @project = Project.find(params[:id])
    end
    
    def deleted_by
      @project.deleted_by = 101
    end