I have a table has a string column contains json data each record is like
{"id":1,"type":"car","name":"bmw"}
{"id":2,"type":"car","name":"Fiat"}
{"id":3,"type":"truck","name":"RAM"}
now I need to make a migration to remove name's element from json string by rails ActiveRecord::Migration[5.0] to becomes like this
{"id":1,"type":"car"}
{"id":2,"type":"car"}
{"id":3,"type":"truck"}
If the database is Postgresql and the column is jsonb
, then assuming you want to operate in the car
table column
<MODEL_NAME>.update_all("car = car - 'name'")
It can be done via Rake task or a data migration
I've done it by rake script
desc 'Remove name'
task remove_name: :environment do
Entry.all.find_each do |entry|
puts "Removing #{entry.id}"
entry.data.delete("name")
entry.save
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With