繁体   English   中英

Append hash 在导轨中排列列

[英]Append hash to array column in rails

我正在使用 postgres 数据库,并且我有一个带有数组类型列的表:

 create_table "reports", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "user_id", null: false
    t.text "assigned_conversation_ids", default: [], array: true
    t.text "first_response_times", default: [], array: true
    t.text "response_times", default: [], array: true
    t.text "close_times", default: [], array: true
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_reports_on_user_id"
  end

我想将 append 对象(散列)添加到 first_response_times 字段。

我尝试了什么:

def update_first_response_time(conversation_id, message_created_at)
  first_response_time = 1000 * (Time.zone.now.to_f - message_created_at.to_f)
  first_response_exists = user_report.first_response_times.select { |first_response_time| JSON.parse(first_response_time).conversation_id == conversation_id }
     
  return if first_response_exists&.size > 0
     
  user_report.first_response_times << `{#{conversation_id}:#{first_response_time}}`
  user_report.save
end

如果具有conversation_id 的键在数组中不存在,我想将append 放到数组中。 我该如何 go 关于这个?

暂无
暂无

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

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