La migración de rieles establece la fecha actual como valor predeterminado

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)

¡Gracias por responder!

Note: I use sqlite in rails 3

preguntado el 01 de febrero de 12 a las 03:02

5 Respuestas

You can pass a lambda for dynamic initializers.

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

Respuesta antigua

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 llamar de vuelta

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 antes de 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 llamar de vuelta

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 y la update.

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

3) Usando el default_value_for joya

class Test
  default_value_for :day do
    Date.today
  end
end

respondido 12 mar '20, 00:03

You can set default date on migration from Rails 5

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

Aquí un enlace from rails repo

Respondido 02 ago 19, 14:08

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

respondido 02 mar '14, 23:03

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.

Respondido 01 Feb 12, 07:02

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

Respondido 01 Feb 12, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.