I'm using postgres database and I have a table like so with array type columns:
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
I want to append objects (hash) to the first_response_times field.
What I tried:
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
I want to append to the array if the key with conversation_id does not already exist in the array. How do I go about this?