I have followed this guide Using Models In Your Migrations. Now I want to add a new column called suspect type boolean into Question table, it will have default value is false, and I want to update records have created too. This is my migration file:
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
But when I run rake db:migrate, it has error:
== 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
I think it needs attr_accesible :suspect, so I added it in Question model, but I still has same error when I run migrate again. I have set this in application.rb too:
config.active_record.whitelist_attributes = true
Anyone has met this problem?
it doesnt work, since you are overwriting the Question Class in your migration.
Remove
and try it again.