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?
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.
The
self.include_inactivemethod is really just for clarity. It is really just an alias ofunscoped, which will reset thedefault_scopethat is ignoring deleted records. If you want to, for example, show all records in an admin dashboard, you can callAttachment.include_inactive.Also note the index on the
activecolumn. Since most queries will include awhere 'active' = 1it is smart to ensure your database has this information easily accessible.