Richard77
Richard77

Reputation: 21651

Is the field value going to be null if I don't add "default" to the migration method?

Here my migration:

def change
  create_table :activities do |t|
    t.integer :week
    t.decimal :jog, :precision => 3, :scale => 1
    t.integer :pushups
    t.integer :situps
    t.decimal :bicycl, :precision => 3, :scale => 1
    t.integer :squats
    t.integer :lunges
    t.integer :ical
    t.integer :bcal
    t.integer :foodid
    t.integer :bmi
    t.decimal :bmr, :precision => 5, :scale => 2
    t.references :user
    t.timestamps
  end

  add_index :activities, :user_id
end

All I want is to have 0 value for those fields when the object is created. Is this the default behavior for ruby or I still need to add default => 0, to make sure that they won't be null? In fact I don't want to start checking if the variable is null or not.

I did some research but I still don't really understand how it works.

Thanks for helping

Upvotes: 0

Views: 154

Answers (1)

Chris Lewis
Chris Lewis

Reputation: 1325

You have to explicitly set the default to be zero for each column, otherwise the default is null/empty.

t.integer :ical, :default => 0

Upvotes: 4

Related Questions