Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DATETIME field to zero date in Rails

Tags:

I have a Rails application that is managing a MySQL database through ActiveRecord. I have a DATETIME column in that database that I would like to set to the zero date. (Not NULL, the zero date. Scroll down that page to see what I mean.)

Seems simple enough, but I cannot figure out how to make this happen without using raw SQL.

Here are a few of the things I have already tried:

  • field = 0 (fails with ActiveRecord error Column 'field' cannot be null)
  • field = "0000-00-00" (fails with error undefined method 'year' for nil:NilClass)
  • field = DateTime.parse("0000-00-00") (fails with DateTime error invalid date)

Is there a way to do this through ActiveRecord, or will I be forced to use raw SQL for this?

EDIT: I am not allowed to alter the structure of my SQL database to fix this. It's a work thing...