{"id":1,"type":"car","name":"bmw"}
{"id":2,"type":"car","name":"Fiat"}
{"id":3,"type":"truck","name":"RAM"}
现在我需要进行迁移,以便通过rails ActiveRecord :: Migration [5.0]从json字符串中删除name' s元素,使其变为这样
{"id":1,"type":"car"}
{"id":2,"type":"car"}
{"id":3,"type":"truck"}
答案 0 :(得分:2)
如果数据库为Postgresql并且列为jsonb
,则假定您要在car
表列中进行操作
<MODEL_NAME>.update_all("car = car - 'name'")
可以通过Rake任务或数据迁移来完成
答案 1 :(得分:1)
我已经通过rake脚本
完成了它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