What is the difference between:
t.boolean :test, :default => true
and
t.boolean :test, :null => true
and
t.boolean :test, :default => true, :null => true
EDIT
Does the following make any sense?
t.boolean :test, :default => true, :null => false
There's no difference. The default value of any reference type is null .
If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.
The null: false parameter means this column does not allow NULL values. The default: false tells us this column will default to false whenever a value isn't specified. This is nice because we can guarantee this field will always be either true or false now. It can't be null because the database prevents it.
null=True will set the field's value to NULL i.e., no data. It is basically for the databases column value.
"null" means "are you allowed to enter a null value in this column"?
Whereas "default" means "if there is a null value in this column ... then use this default value instead"
So, for your examples:
t.boolean :test, :default => true
"this boolean column will insert a true if you don't bother setting a value for it"
t.boolean :test, :null => true
"this boolean column will let you set it to true, false or null - and it will stay the way you set it"
t.boolean :test, :default => true, :null => true
"this boolean column will let you set it to true, false or null... but if you set it to null it will automatically be set to true"
:default - The column’s default value. Use nil for NULL.
:null - Allows or disallows NULL values in the column. This option could have been named :null_allowed.
In the first option, if you don't specify anything, rails will put true In the second option, it will allow the value to be null. In the third option, both apply, the values can be true, false and nil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With