PaulM
PaulM

Reputation: 1

Method value is 'nil' rather than 'true' or 'false' after adding the field to a migration table

I added a field called 'custom_design_enabled' to a 'members' table in my schema.rb file as a boolean with default true. When I attempt to change this in a rails console it says the field is 'nil' rather than either 'true' or 'false'. I think there is a part of the migration process that I have accidentally omitted which gives the method 'custom_design_enabled' a value. Can anyone help me out with this?

Upvotes: 0

Views: 165

Answers (1)

Andrei Savin
Andrei Savin

Reputation: 2474

This is likely happening because you did not specify the default on the new field. The easiest way to fix this is to create a new migration. Create a new migration file (rails g migration change_custom_design_enabled_default) and try this in the migration file:

def up
  change_column :members, :custom_design_enabled, :boolean, default: true
end

def down
  change_column :members, :custom_design_enabled, :boolean, default: nil
end

Alternatively, you can try rolling back the last migration with rake db:rollback and add null: false, default: true to the original migration's add_column method. This might fail if the migration generated used change instead of up and down and rails can't figure out how to undo it, but it's worth a try.

Update: if rollback doesn't work, you can change the original migration to:

def up
  add_column :members, :custom_design_enabled, :boolean
end

def down
  remove_column :members, :custom_design_enabled
end

And do rake db:rollback afterwards.

Upvotes: 0

Related Questions