Search code examples
ruby-on-railsnamed-scope

Rails: Soft Delete


I am fairly new to Rails. I am building a digital library.

I would love to add the option of soft delete to my application.

For now I have added a column delete with boolean datatype to my table and I have also defined the action in my controller for the soft delete, but I am facing issues with defining the scope for the soft delete action in the model, so that the action can be called in the controller.

I want a scope for the soft delete that is false by default, and then when i delete a book, it updates to true in the database.

Controller Action

def softdelete
    @book.softdelete
    respond_to do |format|
      format.html { redirect_to books_url, notice: 'Book was successfully deleted.' }
      format.json { head :no_content }
    end
  end

Soft Delete Migration

class AddDeleteToBooks < ActiveRecord::Migration[5.2]
  def change
    add_column :books, :delete, :boolean, :null =>  true
  end
end

What I have left is how to define the method for soft delete in the book model.


Solution

  • What you would like to do is create a softdelete method, that updates the delete field (I think you need to rename it, since it is a reserved word in Rails)

    class Book < ActiveRecord::Base
      #add a model scope to fetch only non-deleted records
      scope :not_deleted, -> { where(soft_deleted: false) }
      scope :deleted, -> { where(soft_deleted: true) }
    
      #create the soft delete method
      def soft_delete
        update(soft_deleted: true)
      end
    
      # make an undelete method
      def undelete
        update(soft_deleted: false)
      end
    end
    

    And update your controller to fetch only undeleted records from now on

     def index
       @books = Book.not_deleted
     end