Renaming the created_at, updated_at columns of ActiveRecord/Rails

前端 未结 10 669
囚心锁ツ
囚心锁ツ 2020-11-28 07:02

I want to rename the timestamp columns defined in timestamp.rb . Can the methods of timestamp.rb be overwritten? And what has to be done in the application that the module w

10条回答
  •  无人及你
    2020-11-28 07:47

    You could use the beforesave and beforecreate methods to post the DateTime.now to your specified columns.

    class Sample < ActiveRecord::Base
      before_create :set_time_stamps
      before_save :set_time_stamps
    
      private
      def set_time_stamps
        self.created_column = DateTime.now if self.new_record?
        self.updated_column = DateTime.now
      end
    end
    

提交回复
热议问题