created_atは作成日時updated_atは更新日時のタイムスタンプを自動的に作成する
便利なものなのだがupdated_atの値を変更するときに厄介になったので無効にする方法
ActiveRecordに自動更新の設定はあるのでそれを無効にしてしまう
base.record_timestamps = true
record_timestampsでの条件判定をしているのでこれをfalseにすると実行されない
def create_with_timestamps #:nodoc: if record_timestamps t = self.class.default_timezone == :utc ? Time.now.utc : Time.now write_attribute('created_at', t) if respond_to?(:created_at) && created_at.nil? write_attribute('created_on', t) if respond_to?(:created_on) && created_on.nil? write_attribute('updated_at', t) if respond_to?(:updated_at) write_attribute('updated_on', t) if respond_to?(:updated_on) end create_without_timestamps end def update_with_timestamps #:nodoc: if record_timestamps t = self.class.default_timezone == :utc ? Time.now.utc : Time.now write_attribute('updated_at', t) if respond_to?(:updated_at) write_attribute('updated_on', t) if respond_to?(:updated_on) end update_without_timestamps end
ActiveRecordのtimestampsを編集するのはなんとも言い難いのでaction内に定義
ActiveRecord::Base.record_timestamps = false
特定のモデルのみに適応する場合は適応するモデル名に設定
ModelName.record_timestamps = false
特定の処理のみの場合falseにした後trueに戻しておかないと影響がでるやもしれない
ActiveRecord::Base.record_timestamps = true ModelName.record_timestamps = true