Question: Consider the table creation script below:

```
create_table :foo do |t|
  t.datetime :starts_at, :null => false
end

```

Is it's possible to set the default value as the current time?

I am trying to find a DB independent equivalent in rails for the SQL column definitions given below:

**Oracle Syntax**

```
start_at DATE DEFAULT SYSDATE() 

```

**MySQL Syntax**

```
start_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

```

OR

```
start_at DATETIME DEFAULT NOW()

```

Answer: 
You can add a function in a model like this:

```
  before_create :set_foo_to_now
  def set_foo_to_now
    self.foo = Time.now
  end

```

So that the model will set the current time in the model.

You can also place some sql code in the migration for setting the default value at the database level, something like:

```
execute 'alter table foo alter column starts_at set default now()'

```

Setting something like this:

```
create_table :foo do |t|
  t.datetime :starts_at, :null => false, :default => Time.now
end

```

causes executing the Time.now function during migrating so then the table in database is created like this:

```
create table foo ( starts_at timestamp not null default '2009-01-01 00:00:00');

```

but I think that it is not what you want.
If you need to **change** an *existing DateTime column* in Rails 5 (rather than creating a new table as specified in other answers) so that it can take advantage of the default date capability, you can create a migration like this:

```
class MakeStartsAtDefaultDateForFoo < ActiveRecord::Migration[5.0]
  def change
    change_column :foos, :starts_at, :datetime, default: -> { 'CURRENT_TIMESTAMP' }
  end
end

```