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!
.