9
Rails destroy_all cancel
source link: https://www.codesd.com/item/rails-destroy-all-cancel.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Rails destroy_all cancel
advertisements
I've accidentally erased all the records from one my model.
Model.destroy_all
For the output I've received a large list for all the records that have been destroyed.
=> [#<Model id: 1, some_attribute: "Hello World">, #<Model id: 2, some_attribute: " Hello World 2">, etc etc etc]
But, I've got it a text. Can I do anything, using IRB, to return the records back ?
This is very, VERY urgent! Any help is appreciated.
Thank you so much
The following script should do the trick:
require 'bigdecimal'
str = "#<Model id: 1, some_attribute: #<BigDecimal:4ba0730,'0.0',9(18)>, another_attribute: \"Hello World\">, #<Model id: 2, some_attribute: \" Hello World 2\">"
str.scan(/#?<(\w+) (.+?)>(?=, #|$)/) do |m|
model = Object.const_get(m[0])
m[1].gsub!(/#<BigDecimal:.+?('.+?').+?>/, "BigDecimal.new(\\1)")
eval("model.create(#{m[1]})")
end
This also handles instances of BigDecimal. In case you need to handle other special types you can just add another call to gsub!
.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK