by using timestamps we can create created_at and updated_on columns to
our tables. But i dont wnat the columns in my table and i want to add my
own column which datatype is timestamp and that must take
current_timestamp or now as default value.
by using timestamps we can create created_at and updated_on columns to
our tables. But i dont wnat the columns in my table and i want to add my
own column which datatype is timestamp and that must take
current_timestamp or now as default value.
how to do it
re
Since we cannot specify the now() function in the :default option in the
migration file, we can achieve this requirement using the following:
# Sample code
class Samples < ActiveRecord::Migration
def self.up
create_table :system_settings do |t|
t.text :value
t.string :type
t.timestamp :created_time
end
execute "ALTER TABLE system_settings ALTER created_time SET
DEFAULT now()"
end
You have to put the column name in twice as well as specify the datatype
again.
Also, you may be wondering why I am responding to a 7 year old
question... I came across it on google and there was NOTHING else out
there to help with this issue. Yes, I know Rails creates timestamps for
you. However, when you are polling a database that can have information
entered from other sources, it is useful to be able to do this.