-1

Im trying to write a migration in which I have a field to datetime..

Right now my migration for field is like this..

t.datetime :date, default: Time.now

I know that this will create a default time at which migration ran.. and it will be set to all fields..

But what i want is to have a default time at which that row field will be getting created.Like the same as created_at

Nidhin S G
  • 1,685
  • 2
  • 15
  • 45

1 Answers1

1

You can use an Active Record Callback for that, like the before_create

Example:

class Subscription < ActiveRecord::Base
  before_create :record_signup

  private

  def record_signup
    self.signed_up_on = Date.today
  end
end

And you migration won't need a default value.

t.datetime :date

Source and Example: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

iuval
  • 161
  • 2
  • 7