rails/rails — Issue #5354: Could :default be used when the value of a column is nil in mass-assignment ?
Author: simonc
Created: 2012-03-09 13:14:35+00:00
State: open

Say I have the following schema:

``` ruby
create_table "articles", :force => true do |t|
t.string "title", :default => "", :null => false
end
```

When I create an article this way I get an SQL error:

``` ruby
Article.create(title: nil)
```

Shouldn't the default value be used when *the column is not null* AND *a default value is provided* ?

It would prevent app crashes when an attribute is optional but is not supposed to be NULL.
I am always finding myself writing a `before_validation` callback to set the default values.

AFAIK, the initial values when calling `Article.new` are the one defined in the schema.

What do you think and what is the reason it's not working this way ? I guess there is one but I don't know what it is.

Thanks :)