gpt4 book ai didi

mysql - 将无效的 ActiveRecord 日期插入 MySQL 数据库

转载 作者:可可西里 更新时间:2023-11-01 07:55:18 25 4
gpt4 key购买 nike

MySQL 允许 date 字段的值为 0000-00-00,但是 ActiveRecord 将分配给 Date 值为 nil

我有一个遗留应用程序依赖于“从不”概念的确切字段内容,因此如果客户最后付款日期是 0000-00-00,它会确认为“从不”付费。

有没有一种方法可以覆盖此 date 字段的 ActiveRecord(正确)验证并将此值写入数据库而不会弄脏我的手(太)?

感谢您的任何见解。

最佳答案

我在 Rails 3.0.3 项目中遇到了这个问题,猴子修补了以下内容,允许您将“0000-00-00”分配给模型日期字段。该值将在插入/更新时传递到 mysql 数据库列。

  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

关于mysql - 将无效的 ActiveRecord 日期插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1374096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com