Making ActiveRecord / Rails use actual mysql TIMESTAMP columns

后端 未结 2 1017
灰色年华
灰色年华 2021-01-02 04:22

Rails\' :timestamp column type lies; it\'s actually just an alias for :datetime.

I\'m using mysql, and I want to use actual unix-timestamp

相关标签:
2条回答
  • 2021-01-02 05:13

    I'm pretty noobish, but I'll give it a shot. What if you were to add your own custom column, or overwrite the default ones? You can use custom data types with a string like so:

    t.add_column :mysql_timestamp, 'timestamp'
    

    and then somewhere else in your logic

    def mysql_timestamp
      Time.now.strftime("%Y-%m-%d %H:%M:%S")
    end
    

    Not sure about b). Only one way to find out!

    0 讨论(0)
  • 2021-01-02 05:14

    this works (just added whitespace character to type definition, so :timestamp doesn't override it):

         t.add_column :sometable, :created_at, 'timestamp '
    
    0 讨论(0)
提交回复
热议问题