奇怪的铁路迁移行为 - 迁移是成功的,但数据仍然没有填充

时间:2017-11-16 19:22:55

标签: ruby-on-rails ruby-on-rails-4 database-migration postgresql-9.1

以下迁移文件成功运行,没有任何错误,甚至创建了列,但奇怪的是数据没有得到更新。

如果我在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

1 个答案:

答案 0 :(得分:0)

要检查的第一件事是您的development.log文件,一直滚动到底部以获取最新条目。

这里是如何添加列 https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_column

取出所有其他东西并转移到你的模型。