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…).
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).
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.