gpt4 book ai didi

ruby - 拆分数组。 ruby

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:21 26 4
gpt4 key购买 nike

你能帮我解决一些问题吗?我有这样的数据库数组

 str = Hiring::Tag.all
Hiring::Tag Load (0.1ms) SELECT `hiring_tags`.* FROM `hiring_tags`
=> [#<Hiring::Tag id: 1, name: "tag1", created_at: "2013-12-10 11:44:39", updated_at: "2013-12-10 11:44:39">,
#<Hiring::Tag id: 2, name: "tag2", created_at: nil, updated_at: nil>,
#<Hiring::Tag id: 3, name: "tag3", created_at: nil, updated_at: nil>,
#<Hiring::Tag id: 4, name: "wtf", created_at: "2013-12-11 07:53:04", updated_at: "2013-12-11 07:53:04">,
#<Hiring::Tag id: 5, name: "new_tag", created_at: "2013-12-11 10:35:48", updated_at: "2013-12-11 10:35:48">]

我需要像这样拆分这个数组:

    data:[{id:1,name:'tag1'},{id:2,name:'tag2'},
{id:3,name:'tag3'},{id:4,name:'wtf'},{id:5,name:'new_tag'}]

请帮帮我!

最佳答案

如果你使用 ActiveRecord 4.0

Hiring::Tag.pluck(:id, :name).map{ |id, name| {id: id, name: name} }

关于ruby - 拆分数组。 ruby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com