0

我已按照本指南Using Models In Your Migrations进行操作。现在我想在suspectQuestion 表中添加一个名为 type boolean 的新列,它的默认值为 is false,我也想更新已创建的记录。这是我的迁移文件:

class AddSuspectToQuestions < ActiveRecord::Migration
  class Question < ActiveRecord::Base
  end

  def change
    add_column :questions, :suspect, :boolean, default: false
    Question.reset_column_information
    Question.all.each do |question|
      question.update_attributes!(suspect: false)
    end
  end
end

但是当我运行时rake db:migrate,它有错误:

==  AddSuspectToQuestions: migrating ==========================================
-- add_column(:questions, :suspect, :boolean, {:default=>false})
   -> 0.3782s
rake aborted!
An error has occurred, this and all later migrations canceled:

Can't mass-assign protected attributes: suspect

我认为它需要attr_accesible :suspect,所以我在 Question 模型中添加了它,但是当我再次运行 migrate 时仍然有同样的错误。我也设置了这个application.rb

config.active_record.whitelist_attributes = true

有人遇到过这个问题吗?

4

1 回答 1

1

它不起作用,因为您在迁移中覆盖了问题类。

消除

class Question < ActiveRecord::Base
end

再试一次。

于 2012-11-19T12:15:18.403 回答