Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 975
banhbaochay Avatar asked Feb 01 '12 03:02

banhbaochay


People also ask

How do I change migration in Rails?

5 Changing Existing Migrations You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version. In general, editing existing migrations is not a good idea.

What is reversible migration in Rails?

Reversible Migrations Rails allows us to rollback changes to the database with the following command. rails db:rollback. This command reverts the last migration that was run on the database. If the migration added a column event_type then the rollback will remove that column.

How does Rails know which migration to run?

Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them.


2 Answers

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
like image 194
Harish Shetty Avatar answered Sep 28 '22 11:09

Harish Shetty


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

like image 34
Mauro Avatar answered Sep 28 '22 11:09

Mauro