Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting an invalid ActiveRecord date to a MySQL database

MySQL allows the value of 0000-00-00 for date fields, however ActiveRecord treats an assignment to a Date value with that string as being nil.

I have a legacy application that relies on that exact field content for the notion of "Never", so if the customer last payment date is 0000-00-00 it acknowledges as being "Never" paid.

Is there a way I can override ActiveRecord (correct) validation for this date field and write this value to the database without getting my hands (too) dirty?

Thanks for any insight.


2 Answers

I ran into this on a Rails 3.0.3 project and monkey patched the following which allows you to assign '0000-00-00' to a model date field. This value will be carried through to the mysql database column on insert/update.

  class ActiveRecord::ConnectionAdapters::Column
    def self.string_to_date_with_zero_support(string)
      return string if string == '0000-00-00'
      string_to_date_without_zero_support(string)
    end

    class << self
      alias_method_chain(:string_to_date, :zero_support)
    end
  end
like image 79
2 revsDan Lynn Avatar answered Apr 29 '26 16:04

2 revsDan Lynn


You may find this slightly easier and it should achieve the same result without executing the method for every attribute in the model

class CustomerInfo < BillingDatabase
  belongs_to :customer

  def lastpaid
    value = read_attribute(:lastpaid)
    value.blank? ? "0000-00-00" : value
  end
end

You could of course refactor the code to call read_attribute twice and do the lastpaid method as a one-liner. It's a matter of style/minute performance differences really.

def lastpaid
  read_attribute(:lastpaid).blank? "0000-00-00" : read_attribute(:lastpaid)
end
like image 40
Steve Weet Avatar answered Apr 29 '26 18:04

Steve Weet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!