我已遵循本指南Using Models In Your Migrations。现在我想在Question表中添加一个名为suspect
类型boolean的新列,它的默认值为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
,所以我在问题模型中添加了它,但是当我再次运行迁移时仍然有同样的错误。我也在application.rb
中设置了这个:
config.active_record.whitelist_attributes = true
有人遇到过这个问题吗?
答案 0 :(得分:1)
它不起作用,因为你在迁移中覆盖了问题类。
删除
class Question < ActiveRecord::Base
end
再试一次。