Defaulting datetime in migrations

I have the following in a migration:

create_table :orders do |t|
t.references :cart
t.references :user
t.float :item_total, :default => 0.0, :null => false
t.float :discount, :default => 0.0, :null => false
t.float :tax, :default => 0.0, :null => false
t.string :shipping_type
t.float :shipping_cost, :default => 0.0, :null => false
t.float :total, :default => 0.0, :null => false
t.string :tracking
t.datetime :placed_at, :default => ‘0000-00-00 00:00:00’, :null
=> false
t.datetime :shipped_at, :default => ‘0000-00-00 00:00:00’, :null
=> false

  t.timestamps
end

But when I look at scheme.rb I see:

create_table “orders”, :force => true do |t|
t.integer “cart_id”
t.integer “user_id”
t.float “item_total”, :default => 0.0, :null => false
t.float “discount”, :default => 0.0, :null => false
t.float “tax”, :default => 0.0, :null => false
t.string “shipping_type”
t.float “shipping_cost”, :default => 0.0, :null => false
t.float “total”, :default => 0.0, :null => false
t.string “tracking”
t.datetime “placed_at”, :null => false
t.datetime “shipped_at”, :null => false
t.datetime “created_at”
t.datetime “updated_at”
end

As you can see, the “default” value did not take. Yet it did for the
floats that default to 0.0

When I try to save an object of class order I get errors saying the
placed_at and shipped_at can not be null. (Which makes sense giving
what I’ve pasted above).

How can I make those two fields default to “0000-00-00 00:00:00”?

Thanks,
Greg

several possible problems here:

not sure, but maybe you would need to provide a DateTime object instead
of a string.

But however, 0000-00-00 isn’t a valid date anyway, since there is
neither a month 0 nor such a day. It should be 0000-01-01

On Sat, May 31, 2008 at 12:06 AM, Greg [email protected] wrote:

How can I make those two fields default to “0000-00-00 00:00:00”?

The migration might not do it as the migration supports all base
databases
(mysql, postgres and sqlite3). Mysql and Postgres both can take
defaults on
a date/time type column, I am not sure on sqlite3, that could be the
problem.

If you must do this, then you can just do some sql in the migration:

sql = “SOME SQL STATEMENT TO SET THE DEFAULT YOU WANT”
execute(sql)

You can look at http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
for
mysql or
http://www.postgresql.org/docs/8.0/interactive/functions-datetime.html
for
postgres.

Mikel

Well, nothing I’ve tried has worked as far as the migration goes. So
now I’m trying in the model. I have:

class Order < ActiveRecord::Base

before_validation_on_create :assign_dates

validates_presence_of :placed_at
validates_presence_of :shipped_at

private

def assign_dates
self.placed_at = ‘0000-00-00 00:00:00’
self.shipped_at = ‘0000-00-00 00:00:00’
end

end

But when I do a create! action I get: “Validation failed: Shipped at
can’t be blank, Placed at can’t be blank”

Why aren’t those two values being set before validation occurs?

Greg

Why aren’t those two values being set before validation occurs?

as mentioneb above: 0000-00-00 isn’t a valid date in any case

Rails may try to convert this string into a DateTime object with
DateTime.parse or something on that line and that will fail for
0000-00-00.

Ok, setting the date to a valid date “2000-01-01 00:00:00” in the
“assign_dates” method worked. I guess I’m just so used to dealing with
MyPHPAdmin when using PHP - it’s defaults date, datetime and time
fields to all zeros and seems to just always treat them as strings.

Thanks!
Greg