Re: ActiveRecord uses confusing defaults

Jeroen Houben
Fri, 08 Sep 2006 13:27:51 -0000

Rick Olson wrote:
> On 9/8/06, Jeroen Houben
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>> now I would expect this to return nil
>>
>> s.street # => ''''
>> I would expect this to return nil
>>
>> Is there a reason for this?
>>
>> Jeroen
>
> It goes by the defaults set in your database.  What''s your schema
look
> like?
>
> Here''s a snippet from one of my apps:
>
>   create_table "contents", :force => true do |t|
>     t.column "article_id",     :integer
>     t.column "comments_count", :integer,                 :default
=> 0
>   end
>
>>> Content.new.article_id
> => nil
>>> Content.new.comments_count
> => 0

Hey that''s interesting.

    t.column "email", :string, :default => "", :null
=> false
    t.column "fname", :string, :default => "", :null
=> false
    t.column "lname", :string, :default => "", :null
=> false
    t.column "street", :string
    t.column "city", :string, :default => "", :null =>
false
    t.column "shirt_type", :string, :default => "m",
:null => false
    t.column "shirt_size", :string, :default => "l",
:null => false
    t.column "shirt_text", :text, :default => "", :null
=> false
    t.column "processed_at", :datetime
    t.column "approved_at", :datetime
    t.column "created_at", :datetime, :null => false
    t.column "updated_at", :datetime, :null => false
    t.column "housenr", :integer, :default => 0, :null => false
    t.column "housenr_ext", :string

I have all these default that I did not put in there myself. Look at one
of my migrations files:

      t.column "email", :string, :null => false
      t.column "fname", :string, :null => false
      t.column "lname", :string, :null => false
      t.column "address", :string, :null => false
      t.column "postcode", :string, :null => false, :limit => 6
      t.column "city", :string, :null => false
      t.column "shirt_type", :string, :default => "m",
:null => false
      t.column "shirt_size", :string, :default => "l",
:null => false
      t.column "shirt_text", :text, :null => false
      # processed = sent for fulfillment
      t.column "processed_at", :datetime
      t.column "approved_at", :datetime
      # timestamps
      t.column "created_at", :datetime, :null => false
      t.column "updated_at", :datetime, :null => false

Do I have to explicitly set defaults to nil (null?)

Jeroen