banhbaochay
banhbaochay

Reputation: 309

Rails migration set current date as default value

I have a column date in table as:

create_table "test", :force => true do |t|
     t.date  "day"
end

I want to set current date as default value for this column. I try as below:

create_table "test", :force => true do |t|
     t.date  "day", :default => Date.today
end

But default always is Feb 1st, so if I create new record tomorrow, the day still is Feb 1st (expect is Feb 2nd)

Thanks for response!

Note: I use sqlite in rails 3

Upvotes: 16

Views: 14720

Answers (5)

Harish Shetty
Harish Shetty

Reputation: 64363

You can pass a lambda for dynamic initializers.

create_table "test", :force => true do |t|
  t.date  "day", default: -> { 'CURRENT_DATE' }
end

Old answer

Rails does not support dynamic default values in migrations. Whatever is in your migration during its execution will be set at the DB level and will stay that way until the migration is rolled back, overridden, or reset. But you can easily add dynamic defaults at the model level since it's evaluated at runtime.

1) Setting default values using after_initialize callback

class Test
  def after_initialize
    self.day ||= Date.today if new_record?
  end
end

Use this approach only if you need to access the attribute after initialization and before saving the record. This approach has extra processing cost while loading a query result, as the block has to be executed for every result object.

2) Setting default values using before_create callback

class Test
  before_create do
    self.day = Date.today unless self.day
  end
end

This callback is triggered by a create call on your model. There are many more callbacks. For example, setting the date before validation on create and update.

class Test
  before_validation on: [:create, :update] do
    self.day = Date.today
  end
end

3) Using the default_value_for gem

class Test
  default_value_for :day do
    Date.today
  end
end

Upvotes: 34

Mauro
Mauro

Reputation: 1271

You can set default date on migration from Rails 5

create_table :posts do |t|
  t.datetime :published_at, default: -> { 'NOW()' }
end

Here a link from rails repo

Upvotes: 6

squiter
squiter

Reputation: 5761

Just completing Harish Shetty's answer.
For Rails applications, you must to use this syntax:

  class Test < ActiveRecord::Base
    after_initialize do |test|
      test.day ||= Date.today if new_record?
    end
  end

Upvotes: 4

Andrew Cetinic
Andrew Cetinic

Reputation: 2835

I know that Mysql does not accept a default column type as a function. I am assuming that sqlite would be the same.

http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

Upvotes: 0

jdc
jdc

Reputation: 743

Don't think you can do that in a migration. But, Rails already has a created_at field added to new model migrations that does what you want. And if you need your own attribute doing the same thing, just use a before_save or before_validate to set it if it's nil.

Upvotes: 1

Related Questions