Re: ActiveRecord uses confusing defaults

Al Evans
Sat, 09 Sep 2006 12:24:57 -0000

Jeroen wrote:

> class AddTestTable < ActiveRecord::Migration
>   def self.up
>     create_table "contents", :force => true do |t|
>       t.column "email", :string, :null => false
>     end
>   end
>
>   def self.down
>     drop_table "contents"
>   end
> end
>
> $ script/generate model Content
>
> $ script/console
>
> Loading development environment.
>>> Content.new.email
> => ""
>>>
>
> I just dont understand why it defaults to "" and not to nil.
Nowhere did
> I set the default to "" in my migrations file.

It has to default to something.

You''ve prevented it from defaulting to NULL (:null => false). So for
an
string field, it defaults to ''''; for a numeric field it
defaults to 0.

If you WANT to assign nil to a field, you need to remove the ":null =>
false" from your DB specifications.

--Al Evans