Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在ruby中将数组内的散列转换为单键多值散列_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 在ruby中将数组内的散列转换为单键多值散列

Ruby on rails 在ruby中将数组内的散列转换为单键多值散列,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试从redis db加载数据。我有一个只支持api的rails应用程序,并试图按照要求呈现json数据。 目前,我能够以以下格式从redis获取数据 [ { "id": 1, "name": "Stephenie Meyer", "created_at": "2018-04-17T07:40:50.417Z", "updated_at": "2018-04-17T07:40:50.417Z" }, { "id": 2, "name": "V.C. Andre

我正在尝试从redis db加载数据。我有一个只支持api的rails应用程序,并试图按照要求呈现json数据。 目前,我能够以以下格式从redis获取数据

[
 {
  "id": 1,
  "name": "Stephenie Meyer",
  "created_at": "2018-04-17T07:40:50.417Z",
  "updated_at": "2018-04-17T07:40:50.417Z"
 },
 {
  "id": 2,
  "name": "V.C. Andrews",
  "created_at": "2018-04-17T07:40:50.613Z",
  "updated_at": "2018-04-17T07:40:50.613Z"
 },
 {
  "id": 3,
  "name": "Sophie Kinsella",
  "created_at": "2018-04-17T07:40:50.646Z",
  "updated_at": "2018-04-17T07:40:50.646Z"
 }
]
如何将其转换为
name
created
updated
的键值对将散列为
id
键值对

进入这个

 {"id": 1,
    {
      "name": "Stephenie Meyer",
      "created_at": "2018-04-17T07:40:50.417Z",
      "updated_at": "2018-04-17T07:40:50.417Z"
    }
 }
获取redis数据的助手方法

def fetch_authors
    authors = $redis.get('authors')
    if authors.nil?
      authors = Author.all.to_json
      $redis.set("authors", authors).to_json
      $redis.expire("authors", 5.hour.to_i)
    end
    JSON.load authors
end
并使用

def index
    @authors = fetch_authors
    render json: @authors
  end

最接近您想要的可能是:

input = ...
input.map { |hash| [hash.delete(:id) || hash.delete('id'), hash] }.to_h

#⇒ {{1=>{:name=>...},
#   {2=>{:name=>...},
#   {3=>{:name=>...}}

这并不完全是您想要的,因为这不是正确的语法,但您可以通过

这会回来的

{
    1 => [
        {
                    :id => 1,
                  :name => "Stephenie Meyer",
            :created_at => "2018-04-17T07:40:50.417Z",
            :updated_at => "2018-04-17T07:40:50.417Z"
        }
    ],
    2 => [
        {
                    :id => 2,
                  :name => "V.C. Andrews",
            :created_at => "2018-04-17T07:40:50.613Z",
            :updated_at => "2018-04-17T07:40:50.613Z"
        }
    ],
    3 => [
        {
                    :id => 3,
                  :name => "Sophie Kinsella",
            :created_at => "2018-04-17T07:40:50.646Z",
            :updated_at => "2018-04-17T07:40:50.646Z"
        }
    ]
}

“类似”的内容不是有效的ruby,因此无法进行转换。
[null,{“id”:1,“name”:“Stephenie Meyer”,“created_at:“2018-04-17T07:40:50.417Z”,“updated_at:“2018-04-17T07:40:50.417Z”},这就是它正在生成的内容。不管怎样,我可以用id代替
null
?@user3576036这不是结果,而是结果
{1=>{:name=>“Stephenie Meyer”,:created_at=>“2018-04-17T07:40:50.417Z”,:updated_at=>“2018-04-17T07:40:50.417Z”},2=>{:name=>“V.C.Andrews”,:created_at=>“2018-04-17T07:40:50.617z=>“2018-04-17T07:40:50.613Z”},3=>{:name=>“Sophie Kinsella”,:created_at=>“2018-04-17T07:40:50.646Z”,:updated_at=>“2018-04-17T07:40:50.646Z”}
好的。我当时没有得到id号。它给出的值为空。
@authors
是我应该给出的数组输入。@Stefan这将是一个很棒的会议闪电演讲标题:“缺少咖啡是hashrockets的主要原因。”:)因为
JSON.load authors
返回了相同的数组。我在
@authors
数组上调用了
group_by
。但是没有变化。@user3576036
group_by
不会修改它的接收器(数组不能转换为哈希)。它会返回一个新数组。
{
    1 => [
        {
                    :id => 1,
                  :name => "Stephenie Meyer",
            :created_at => "2018-04-17T07:40:50.417Z",
            :updated_at => "2018-04-17T07:40:50.417Z"
        }
    ],
    2 => [
        {
                    :id => 2,
                  :name => "V.C. Andrews",
            :created_at => "2018-04-17T07:40:50.613Z",
            :updated_at => "2018-04-17T07:40:50.613Z"
        }
    ],
    3 => [
        {
                    :id => 3,
                  :name => "Sophie Kinsella",
            :created_at => "2018-04-17T07:40:50.646Z",
            :updated_at => "2018-04-17T07:40:50.646Z"
        }
    ]
}