下面的迁移文件成功运行,没有任何错误,甚至创建了列,但奇怪的是数据没有得到更新。
如果我在 rails 控制台中运行代码(忽略创建列(,它的效果非常好。任何解释:
class AddSubjectListToTestType < ActiveRecord::Migration
def change
add_column :test_types, :subject_list, :integer, array:true, default: []
type_hash = {
"NEET" => ["Physics" , "Chemistry", "Botany", "Zoology"],
"JEE" => ["Physics", "Chemistry", "Mathematics"],
"CET" => ["Physics", "Chemistry", "Mathematics"]
}
TestType.all.each do |test_type|
type_hash[test_type.name].each do |subject|
test_type.subject_list << Subject.find_by_name(subject).id
end
test_type.save
end
end
end
首先要检查的是您的开发.log文件,一直滚动到底部以获取最新条目。
添加列的方法如下https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_column
把所有其他东西都拿出来,移动到你的模型上。