Forum: Ruby on Rails how to add current time as default value

4268e0387b4a3da4b9de2bb46f18dba6?d=identicon&s=25 Rajkumar Surabhi (mailtorajuit)
on 2010-03-19 09:16
hi all,

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
9273517c02d97cf7e0b29b5f38e78231?d=identicon&s=25 Loganathan Ganesan (loganathanbk)
on 2010-03-19 10:14
Rajkumar Surabhi wrote:
> hi all,
>
> 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

  def self.down
  end
end
E1957b5e4fc7128511d167e79dd499da?d=identicon&s=25 kannav rajeev (Guest)
on 2010-03-19 10:24
(Received via mailing list)
Time.now

On Fri, Mar 19, 2010 at 2:44 PM, Loganathan Ganesan
<lists@ruby-forum.com>wrote:

> >
>      t.text  :value
>
> 
rubyonrails-talk+unsubscribe@googlegroups.com<rubyonrails-talk%2Bunsubscribe@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>


--
Thanks:
Rajeev sharma
6a0573ae7357c194b70be0b9f6575307?d=identicon&s=25 JoeEd E. (joeed_e)
on 2013-07-02 05:42
If Loganathan's answer does not work, try this:

I had to use

execute "ALTER TABLE system_settings ALTER created_time SET
DEFAULT CURRENT_TIMESTAMP"

(instead of NOW())
6a0573ae7357c194b70be0b9f6575307?d=identicon&s=25 JoeEd E. (joeed_e)
on 2013-07-02 06:02
Okay, so it turns out that does not work either. Here is the actual
syntax:

  execute "ALTER TABLE tablename CHANGE column_name column_name
TIMESTAMP DEFAULT CURRENT_TIMESTAMP"

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 3 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.