保存记录时如何保持数组结构 - activerecord / postgres

时间:2016-10-06 12:11:34

标签: ruby-on-rails arrays postgresql activerecord

我创建了一个哈希的数组结构,我在postgres中创建了一个列,使用下面的迁移来保存该结构

class AddKeyDirectionsToEvent < ActiveRecord::Migration[5.0]
  def change
    add_column :calendar_events, :key_directions, :text, array:true, default: []
  end
end

现在,数组的结构是

下面的结构
{
    :in => [
        [0] {
               :duration => "5 mins",
               :distance => "0.4 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        },
        [1] {
               :duration => "12 mins",
               :distance => "5.3 km",
            :travel_mode => "TRANSIT",
            :travel_type => "SUBWAY"
        },
        [2] {
               :duration => "9 mins",
               :distance => "0.7 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        }
    ]
}
{
    :out => [
        [0] {
               :duration => "10 mins",
               :distance => "0.7 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        },
        [1] {
               :duration => "12 mins",
               :distance => "5.3 km",
            :travel_mode => "TRANSIT",
            :travel_type => "SUBWAY"
        },
        [2] {
               :duration => "6 mins",
               :distance => "0.4 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        }
    ]
}

但由于某种原因,在数据库中它会像这样保存

["{:in=>[{:duration=>\"5 mins\", :distance=>\"0.4 km\", :travel_mode=>\"WALKING\", :travel_type=>nil}, {:duration=>\"12 mins\", :distance=>\"5.3 km\", :travel_mode=>\"TRANSIT\", :travel_type=>\"SUBWAY\"}, {:duration=>\"9 mins\", :distance=>\"0.7 km\", :travel_mode=>\"WALKING\", :travel_type=>nil}]}", "{:out=>[{:duration=>\"10 mins\", :distance=>\"0.7 km\", :travel_mode=>\"WALKING\", :travel_type=>nil}, {:duration=>\"12 mins\", :distance=>\"5.3 km\", :travel_mode=>\"TRANSIT\", :travel_type=>\"SUBWAY\"}, {:duration=>\"6 mins\", :distance=>\"0.4 km\", :travel_mode=>\"WALKING\", :travel_type=>nil}]}"]

任何想法为什么?我尝试将数组类型从:text转换为:varchar并获得相同的结果。我找到的唯一解决方案是使用eval命令将字符串转换回不理想的数组。

1 个答案:

答案 0 :(得分:1)

您需要将列类型设置为json.Use,位于迁移命令 -

下方
rails g migration AddKeyDirectionsToCalendarEvent key_directions:json

将您的结构转换为ruby数组,如下所示:

data = [{
    :in => [
        {
               :duration => "5 mins",
               :distance => "0.4 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        },
        {
               :duration => "12 mins",
               :distance => "5.3 km",
            :travel_mode => "TRANSIT",
            :travel_type => "SUBWAY"
        },
        {
               :duration => "9 mins",
               :distance => "0.7 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        }
    ]
},
{
    :out => [
        {
               :duration => "10 mins",
               :distance => "0.7 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        },
        {
               :duration => "12 mins",
               :distance => "5.3 km",
            :travel_mode => "TRANSIT",
            :travel_type => "SUBWAY"
        },
        {
               :duration => "6 mins",
               :distance => "0.4 km",
            :travel_mode => "WALKING",
            :travel_type => nil
        }
    ]
}]

然后呢,

event.key_directions=data
event.save

将保存在数据库中,如下所示,

[["updated_at", 2016-10-06 13:11:26 UTC], ["key_directions", "[{\"in\":[{\"duration\":\"5 mins\",\"distance\":\"0.4 km\",\"travel_mode\":\"WALKING\",\"travel_type\":null},{\"duration\":\"12 mins\",\"distance\":\"5.3 km\",\"travel_mode\":\"TRANSIT\",\"travel_type\":\"SUBWAY\"},{\"duration\":\"9 mins\",\"distance\":\"0.7 km\",\"travel_mode\":\"WALKING\",\"travel_type\":null}]},{\"out\":[{\"duration\":\"10 mins\",\"distance\":\"0.7 km\",\"travel_mode\":\"WALKING\",\"travel_type\":null},{\"duration\":\"12 mins\",\"distance\":\"5.3 km\",\"travel_mode\":\"TRANSIT\",\"travel_type\":\"SUBWAY\"},{\"duration\":\"6 mins\",\"distance\":\"0.4 km\",\"travel_mode\":\"WALKING\",\"travel_type\":null}]}]"], ["id", 4]]

当您访问它时,您将获得数组

    event.key_directions

 => [{"in"=>[{"duration"=>"5 mins", "distance"=>"0.4 km", "travel_mode"=>"WALKING", "travel_type"=>nil}, {"duration"=>"12 mins", "distance"=>"5.3 km", "travel_mode"=>"TRANSIT", "travel_type"=>"SUBWAY"}, {"duration"=>"9 mins", "distance"=>"0.7 km", "travel_mode"=>"WALKING", "travel_type"=>nil}]}, {"out"=>[{"duration"=>"10 mins", "distance"=>"0.7 km", "travel_mode"=>"WALKING", "travel_type"=>nil}, {"duration"=>"12 mins", "distance"=>"5.3 km", "travel_mode"=>"TRANSIT", "travel_type"=>"SUBWAY"}, {"duration"=>"6 mins", "distance"=>"0.4 km", "travel_mode"=>"WALKING", "travel_type"=>nil}]}]