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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 > > > reSince 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Time.now On Fri, Mar 19, 2010 at 2:44 PM, Loganathan Ganesan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> 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 > > > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Thanks: Rajeev sharma -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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()) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/871cc807ad0596a5ddec955848b85f85%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
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 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1ee7a581fbe17410589863813be92bb2%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.