ActiveRecord uses confusing defaults


I have an AR model called Submission
In my submissions table (mysql 5) I have a column called postcode
(varchar, not null) and a column street (varchar, not null)

s =
s.postcode # => 0

now I would expect this to return nil

s.street # => ''
I would expect this to return nil

Is there a reason for this?


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

=> nil

=> 0

Hey that's interesting.

    t.column "email", :string, :default => "", :null => false

So my point was, setting the column default to "" means it's default
upon AR model initialization. If you set this up with :null => false
(or NOT NULL), your DB probably won't let you save NULL to the
attribute. Quit setting these confusing defaults, and you'll stop
seeing them in AR.


You aren’t going nuts - it’s just AR trying to make it’s best interpretation of what it gets and this may very well be an edge case that could be up for discussion.

First, the migrations are simply used to create your table - they bear not on the actual model classes used later on. AR makes a call to “show fields” on the database and creates the column classes from there. Where you are running into an issue is that show fields returns a blank string for both of your example fields and AR makes it’s best guess from there based on the field type. For the string field - AR would have no choice but to do as it does because a blank string is a perfectly valid option for a string field default and therefore it needs to handle that case in that way. The integer column is a different story and you raise what I think would be an interesting idea that since a blank string is not a zero - then AR should not be making that leap in it’s handling of the default values. You might want to see what “show fields” would give back if you specifically gave your columns a nil (null) default value.

The short term solution to your problem should you need the nil values would be to override the columns method of the base class and put in the default values you wish for the columns

For example (completely untested so don’t be surprised if there’s bugs)


class Content < ActiveRecord::Base

def columns
unless @columns
super! { |col|
if == ‘email’
col =, #default value#, col.sql_type, col.null)

Obviously the #default value# would get replaced by the exact value you wanted for the column. Note this is pretty brute force but it should work just fine for you if you needed it. It also locks the model into MySQL due to the need for the MysqlColumn class. It also just doesn’t have that smooth look of rails :slight_smile:

Hope this helps you understand how it works and why

John W Higgins