add_column :default=>now() ?

I don't think you can do this in migrations because the database is not that smart. But, you can do it easily in your model with before_create

before_create :set_foo_to_now

def set_foo_to_now   self.foo = Time.now end

Now, every time you create a record, it will set the field 'foo' to Time.now

before_create :set_foo_to_now

def set_foo_to_now   self.foo = Time.now end

Also, if you use the magic column name 'created_at', rails will auto timestamp your record on creation.

See also http://wiki.rubyonrails.org/rails/pages/MagicFieldNames

Joe Ruby wrote:

Right -- that's what I currently do. Seems like there should be a way to get the string 'current_timestamp' passed through to the database without it getting interpreted. Like a default value for string/text columns (haven't played around with that yet).

Joe, the problem with what you are proposing is that the default option in your migration actually sets the default on the database column. This default value is never interpreted by ruby after you run migrate -- it exists in the database table definition itself. I don't know of any database that allows for a dynamic default value as you are proposing. I'm most familiar with MySQL ... maybe you can set the default to the MySQL function NOW() ... but I don't know if that would work, and it ceratinly wouldn't work if you ever deployed your app on another database type. Try it, I'd be interested to see how it works.

I'm pretty sure that the best way to do this is with before_create or magic columns as was already said.

Joe Ruby wrote:

Right -- that's what I currently do. Seems like there should be a way to get the string 'current_timestamp' passed through to the database without it getting interpreted. Like a default value for string/text columns (haven't played around with that yet).

Joe, the problem with what you are proposing is that the default option in your migration actually sets the default on the database column. This default value is never interpreted by ruby after you run migrate -- it exists in the database table definition itself. I don't know of any database that allows for a dynamic default value as you are proposing.

Postgresql does it...

test=# create table foo (str varchar(32), ts timestamp default current_timestamp); CREATE TABLE test=# \d foo                    Table "public.foo"   Column | Type | Modifiers