2016-08-11 47 views
2

我在tasks表中有一個屬性,它的名字爲is_active,它是boolean。但是我忘了在創建表時將默認值設置爲這個屬性。所以現在我是試圖設置這個值默認爲true,但正如我可以從sqlite瀏覽器看到的 - 新任務仍然創建與此值設置爲NULL。請你幫我找到我的遷移問題在哪裏?MIgration沒有工作軌道

遷移文件:

class AddDefaultValueToTask < ActiveRecord::Migration[5.0] 
    def change 
    def up 
     change_column :tasks, :is_active, :boolean, :default => true 
    end 

    def down 
     change_column :tasks, :is_active, :boolean, :default => nil 
    end 
    end 
end 

Schema.rb文件

create_table "tasks", force: :cascade do |t| 
    t.text  "body" 
    t.boolean "is_active" 
    t.integer "project_id" 
    t.string "deadline" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.index ["project_id"], name: "index_tasks_on_project_id" 
    end 

回答

2

你缺少哈希火箭運營商: '=>'

change_column :tasks, :is_active, :boolean, :default => true 

此外,如果您使用的是updown那麼你需要刪除change塊。這兩種方法不能同時使用

+0

顛倒遷移,添加hashrocket,仍然沒有變化 – Mikhah

+0

嘗試取出'def change'塊,並且只留下'up'和'down'塊。看看是否有幫助 – Ren

+0

是的,問題解決了,謝謝 – Mikhah