简体   繁体   English

Ruby on Rails mysql迁移使用时区而不是日期时间

[英]Ruby on Rails mysql migrate use Timezone instead of Datetime

I am keeping in my mysql table a column called 我在mysql表中保留了一个名为

starts_at 开始于

my column can be with a different timezone each time. 我的专栏每次都可以使用不同的时区。

I have tried creating a migration: 我尝试创建迁移:

def self.up
    create_table :meetings, id: false, force: true do |t|
         t.string       :id
         t.timestamp    :starts_at
    end
end

but i always end up with a 但我总是以

datetime 约会时间

column type 列类型

i want to force rails to use timestamp , and tried all of these with with no success : 我想迫使rails使用timestamp ,并尝试了所有这些都没有成功

def up
    change_column :meetings, :starts_at, :timestamp
end

this: 这个:

def up
    change_column :meetings, :starts_at, 'timestamp'
end

and this: 和这个:

 def self.up
    create_table :meetings, id: false, force: true do |t|
         t.string       :id
         t.column       :starts_at, 'timestamp'
    end
end

any ideas? 有任何想法吗?

update: 更新:

i want to save the timezone on the database record and not on the application configuration 我想将时区保存在数据库记录中,而不是在应用程序配置中

Rails actually makes some of decisions for you. Rails实际上会为您做出一些决策。 Both :timestamp and :datetime will default to DATETIME , while :date and :time corresponds to DATE and TIME , respectively. :timestamp:datetime都默认为DATETIME ,而:date:time对应于DATETIME

Using Rails, you only have to decide whether you need to store date, time or both. 使用Rails,您只需决定是否需要存储日期,时间或两者都存储。

So you can use type :datetime in your :starts_at column. 因此,您可以在:starts_at列中使用:datetime类型。

Look at this example in config/application.rb 在config / application.rb中查看此示例

class Application < Rails::Application
   config.time_zone = 'Sydney'
   config.time_zone = 'Kuala Lumpur'
   config.active_record.default_timezone = 'Kuala Lumpur'
end

You can set your Time zone according to requirement and you can make it dynamic. 您可以根据需要设置时区,并使其动态。

Here is all timezone ActiveSupport::TimeZone.all.map(&:name) 这是所有时区ActiveSupport::TimeZone.all.map(&:name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM