Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:08:54+00:00 2026-05-23T16:08:54+00:00

I am trying to provide undo functionality after a record has been deleted. To

  • 0

I am trying to provide undo functionality after a record has been deleted. To do this, before I delete the record I clone it and put it in the session like so:

session[:undo] ||= []
session[:undo] << record.clone

however when I later do

rec = session[:undo][-1]
rec.save!

The record is not saved. The server console output says

(0.2ms)  BEGIN
[paperclip] Saving attachments.
(0.2ms)  COMMIT

which makes me think it is saved, but its not in the db. I am very confused. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:08:55+00:00Added an answer on May 23, 2026 at 4:08 pm

    A more stable way of doing this is to have an “active” boolean flag in the database that defaults to true. When you delete a record, just change that flag to false. To undo you just change it back to true.

    # models/attachment.rb
    class Attachment < ActiveRecord::Base
      default_scope where(:active => true)
    
      def self.include_inactive
        unscoped
      end
    end
    
    # in your migration
    def self.up
      change_table :attachments do |t|
        t.boolean :active, :default => true, :null => false
      end
    
      add_index :attachments, :active
    end
    

    The self.include_inactive method is really just for clarity. It is really just an alias of unscoped, which will reset the default_scope that is ignoring deleted records. If you want to, for example, show all records in an admin dashboard, you can call Attachment.include_inactive.

    Also note the index on the active column. Since most queries will include a where 'active' = 1 it is smart to ensure your database has this information easily accessible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the situation. I'm trying to provide a service where someone embedds an
I'm trying to provide an IBAction method for common functionality that is required at
I'm trying to provide reporting functionality on a typical restaurant type database. I describe
I'm trying to provide a functionality of having two Methods one called StartTask(action mymethod)
I'm trying to provide a link to my company's website from a Windows Form.
I am trying to provide my own labelFunction for a CategoryAxis programatically but am
I am trying to provide a simple RESTful API to my ASP MVC project.
I am trying to provide an interface to an abstract generic base class. I
Trying to use an excpetion class which could provide location reference for XML parsing,
I'm trying to use ThreadLocal to provide thread safety to pre-existing non-thread-safe classes, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.