Add_column :default=>now()?

How can I add a column – using migrations – so that its default comes
out as either ‘current_timestamp’ or ‘now()’? If I use
‘:default=>Time.now’ or ‘:default=>‘current_timestamp’’, the timestamp
of the current actual time gets used, which is completely useless. I
want the default to be the timestamp of when the record is actually
created (yes, I know I can use ‘created_on’ and so on, but…).

Thanks!
Joe

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

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

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


Yan

http://planyp.us

Joe R. wrote:

(yes, I know I can use ‘created_on’ and so on, but…).

Yan Pritzker wrote:

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


Yan
http://skwp.wordpress.com
http://planyp.us

Nate C. wrote:

Joe R. 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.

Yep, I know how defaults in migrations work (except of course for
sticking current_timestamp in the db unchanged).

I’m certain I used now() with mysql. I think PostgreSQL allows the use
of any function (like user-created ones), and I KNOW now(),
current_timestamp, and others work.

Joe

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
--------±----------------------------±--------------
str | character varying(32) |
ts | timestamp without time zone | default now()

test=# insert into foo (str) values (‘One’);
INSERT 0 1
test=# insert into foo (str) values (‘Two’);
INSERT 0 1
test=# select * from foo;
str | ts
-----±---------------------------
One | 2006-08-23 18:28:24.819711
Two | 2006-08-23 18:28:31.61082
(2 rows)

I’m most familiar with MySQL … maybe you can set the

Joe R. 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.