Rails Rails 自定义 created_at 和 updated_at 列

alixiaomiao · March 28, 2017 · Last by 5swords replied at August 15, 2017 · 1658 hits

通过覆盖下面两个方法可以实现,虽然使用场景不多,但也是个 Feature

class User < ActiveRecord::Base

  # 非必需,但是 Rails 源码是 Private 的,尽量保持一致  
  private

  def timestamp_attributes_for_create
    ["created_at", "created_on"]
  end

  def timestamp_attributes_for_update
    ["updated_at", "updated_on"]
  end
end

Rails 源码在这 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/timestamp.rb#L69-L75

希望我不是最后一个知道的。 😅

It's a hack, not feature.

这是 class_methods

You need to Sign in before reply, if you don't have an account, please Sign up first.