Mikhah Mikhah - 3 months ago 18
SQL Question

MIgration didn't work rails

I have an attribute in

tasks
table, it's named
is_active
and it is
boolean
.But I've forgot to set the default value to this attribute when I was creating a table.So now I'm trying to set this value to
true
by default,but as I can see from sqlite browser - new tasks are still creating with this value set to
NULL
.Can you please help me to find where is the problem in my migration?

Migration file:

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 file

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

Ren Ren
Answer

You are missing the hash rocket operator: '=>'

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

Also, if you are using up and down then you need to delete the change block. Both methods cannot be used at the same time