I deleted some data in a database but I want those data back.
Is there any way to restore the data from a database that was deleted using the rails console?
I deleted some data in a database but I want those data back.
Is there any way to restore the data from a database that was deleted using the rails console?
Unfortunately, you cannot recover data using the console tool unless previously deleted objects have been saved to a variable or elsewhere.
No, you cannot recover any data that was deleted using the rails console. You will need a backup. If you don't have a backup, you will not be able to restore the data.
However, depending on how you removed your data, you might have had a chance to recover it at the moment you deleted it.
If you used ActiveRecord code like the following
Rails.application.eager_load!
ActiveRecord::Base.connection.disable_referential_integrity do
ApplicationRecord.descendants.each do |model|
model.delete_all
end
end
The data is lost. (Have a look at this question and the answers about different ways to truncate data)
If you did something like this:
objects = MyModel.delete_all
then you might have had a chance to recover them right after that using the objects
array.