Rails Rails 5 and PostgreSQL 'Interval' data type?

403 · December 06, 2018 · Last by jasl replied at December 07, 2018 · 1153 hits

rails 5 中直接是 string 类型

class AddUpdateCycleToCatalogs < ActiveRecord::Migration[5.0]
  def change
    add_column :catalogs, :update_cycle, :interval  , default: 1.year
  end
end

t.string   "update_cycle",      default: "8766:00:00"

这样的话,查询出来就直接是"8766:00:00" 类的数据 .....这是不是就有点蛋疼了,,怎么处理优雅一点?

可以试试显式声明字段类型 attribute :update_cycle, :time

https://api.rubyonrails.org/classes/ActiveRecord/Type.html

如果内置的都不能适应场景的话,就得自己扩展了

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